結果

問題 No.1420 国勢調査 (Easy)
ユーザー logxlogx
提出日時 2021-01-29 20:53:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,182 bytes
コンパイル時間 3,198 ms
コンパイル使用メモリ 205,440 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 14:11:46
合計ジャッジ時間 6,675 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 29 ms
4,380 KB
testcase_03 AC 29 ms
4,380 KB
testcase_04 AC 29 ms
4,380 KB
testcase_05 AC 29 ms
4,380 KB
testcase_06 AC 29 ms
4,380 KB
testcase_07 AC 28 ms
4,380 KB
testcase_08 AC 28 ms
4,376 KB
testcase_09 AC 29 ms
4,380 KB
testcase_10 AC 29 ms
4,380 KB
testcase_11 AC 28 ms
4,376 KB
testcase_12 AC 5 ms
4,384 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 11 ms
4,384 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 11 ms
4,504 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 11 ms
4,384 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 40 ms
4,384 KB
testcase_23 AC 41 ms
4,380 KB
testcase_24 AC 40 ms
4,376 KB
testcase_25 AC 41 ms
4,384 KB
testcase_26 AC 41 ms
4,380 KB
testcase_27 AC 34 ms
4,380 KB
testcase_28 AC 32 ms
4,380 KB
testcase_29 AC 32 ms
4,384 KB
testcase_30 AC 32 ms
4,380 KB
testcase_31 AC 32 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
const char newl='\n';

//Tには可換群が載る.
template<typename T,T (*op)(T,T),T (*op_inv)(T,T)>
struct weight_uf{
    int n;
    vector<int> par;
    vector<T> diff;
    weight_uf(const int _):n(_),par(_,-1),diff(_,T(0)){}
    //O(α(N))
    int find(const int x){
        if(par[x]<0)return x;
        int r=find(par[x]);//根 この時に先祖のdiffの値も更新がかかる
        diff[x]=op(diff[x],diff[par[x]]);
        return par[x]=r;
    }
    //O(α(N))
    T weight(const int x){
        find(x);
        return diff[x];
    }
    //!same(x,y)なる(x,y)に対しては動かない.
    //O(α(N))
    T diff_query(int x,int y){
        return op_inv(weight(y),weight(x));
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
    /*
    既にある情報と矛盾する場合はreturn falseする.
    diff[y]-diff[x]=wとなるようにunite.
    */
    //O(α(N))
    bool unite(int x,int y,T w){
        if(same(x,y)){
            //矛盾する
            if(diff_query(x,y)!=w)return false;
            //矛盾しない
            else return true;
        }
        w=op(w,op_inv(weight(x),weight(y)));
        x=find(x);
        y=find(y);
        if(par[x]>par[y])swap(x,y),w=op_inv(T(0),w);
        int t=par[y]+par[x];
        par[x]=t;
        par[y]=x;
        diff[y]=w;
        n--;
        return true;
    }
    //O(1)
    int getsize(int i){
        i=find(i);
        return -par[i];
    }
};

int op(int a,int b){
    return a^b;
}
int op_inv(int a,int b){
    return a^b;
}


int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n,m;cin >> n >> m;
    weight_uf<int,op,op_inv> uf(n);
    bool ok=true;
    while(m--){
        int a,b;cin >> a >> b;
        int y;cin >> y;
        ok&=uf.unite(a-1,b-1,y);
    }
    if(!ok){
        cout << -1 << newl;
        return 0;
    }
    vector<int> ans(n);
    rep(i,n){
        if(uf.find(i)==i)ans[i]=0;
    }
    rep(i,n){
        int r=uf.find(i);
        ans[i]=ans[r]^uf.diff_query(i,r);
    }
    for(auto &e:ans)cout << e << newl;
}
0