AtCoder Grand Contest 016

Submission #1389794

Source codeソースコード

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define REP(i, a, b)  for(int i = (a), i##end = (b); i < i##end; ++i)
#define DREP(i, a, b) for(int i=(a-1), i##end = (b); i >=i##end; --i)

template <typename T> bool chkmax(T& a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T& a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;
const int maxn = 100000 + 10;

template<typename T> T read() {
    T n(0), f(1);
    char ch = getchar();
    for( ;!isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for( ; isdigit(ch); ch = getchar()) n = n * 10 + ch - 48; 
    return n * f;
}

int n;
int a[maxn];
int b[maxn];
vector<int> A, B, G[maxn];

int main() {
#ifdef Wearry
    freopen("data.txt", "r", stdin);
    freopen("ans.txt", "w", stdout);
#endif

    n = read<int>();
    for(int i = 1; i <= n; i++) a[0] ^= (a[i] = read<int>()), A.pb(a[i]);
    for(int i = 1; i <= n; i++) b[0] ^= (b[i] = read<int>()), B.pb(b[i]);

    A.pb(a[0]); sort(A.begin(), A.end());
    B.pb(b[0]); sort(B.begin(), B.end());

    for(int i = 0; i <= n; i++) if(A[i] != B[i]) return puts("-1"), 0; 

    static bool equ[maxn], vis[maxn];

    A.resize(unique(A.begin(), A.end()) - A.begin());

    for(int i = 1; i <= n; i++) equ[i] = (a[i] == b[i]);
    for(int i = 0; i <= n; i++) a[i] = lower_bound(A.begin(), A.end(), a[i]) - A.begin();
    for(int i = 0; i <= n; i++) b[i] = lower_bound(A.begin(), A.end(), b[i]) - A.begin();
    for(int i = 0; i <= n; i++) if(!equ[i]) G[a[i]].pb(i);

    int ans = 0;
    for(int i = 0; i <= n; i++) if(!equ[i] && !vis[i]) {
        static int q[maxn];
        int head = 0, tail = 0;

        vis[q[tail++] = i] = 1;
        while(head < tail) {
            int x = q[head++];
            for(const auto& to : G[b[x]]) 
                if(!vis[to]) vis[to] = 1, q[tail++] = to;
        }

        ans += i ? tail+1 : tail-1;
    }
    printf("%d\n", ans);
    return 0;
}

Submission

Task問題 D - XOR Replace
User nameユーザ名 Wearry
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 IE
Score得点 -
Source lengthソースコード長 2141 Byte
File nameファイル名
Exec time実行時間 -
Memory usageメモリ使用量 -

Test case