結果

問題 No.1420 国勢調査 (Easy)
ユーザー logxlogx
提出日時 2021-01-29 20:51:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,046 bytes
コンパイル時間 2,615 ms
コンパイル使用メモリ 205,420 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 14:10:39
合計ジャッジ時間 9,262 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 29 ms
4,360 KB
testcase_08 AC 29 ms
4,356 KB
testcase_09 AC 28 ms
4,356 KB
testcase_10 AC 28 ms
4,356 KB
testcase_11 AC 30 ms
4,356 KB
testcase_12 AC 6 ms
4,356 KB
testcase_13 WA -
testcase_14 AC 6 ms
4,356 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 33 ms
4,356 KB
testcase_28 AC 33 ms
4,356 KB
testcase_29 AC 33 ms
4,352 KB
testcase_30 AC 33 ms
4,352 KB
testcase_31 AC 33 ms
4,356 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';

struct weight_uf{
    int n;
    vector<int> par;
    vector<int> diff;
    weight_uf(const int _):n(_),par(_,-1),diff(_,0){}
    //O(α(N))
    int find(const int x){
        if(par[x]<0)return x;
        int r=find(par[x]);//根 この時に先祖のdiffの値も更新がかかる
        diff[x]=diff[x]^diff[par[x]];
        return par[x]=r;
    }
    //O(α(N))
    int weight(const int x){
        find(x);
        return diff[x];
    }
    //!same(x,y)なる(x,y)に対しては動かない.
    //O(α(N))
    int diff_query(int x,int y){
        return 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,int_least16_t w){
        if(same(x,y)){
            //矛盾する
            if(diff_query(x,y)!=w)return false;
            //矛盾しない
            else return true;
        }
        w=w^weight(x)^weight(y);
        x=find(x);
        y=find(y);
        if(par[x]>par[y])swap(x,y);
        int t=par[y]+par[x];
        par[x]=t;
        par[y]=x;
        diff[y]=w;
        n--;
        return true;
    }
    //O(α(N))
    int getsize(int i){
        i=find(i);
        return -par[i];
    }
};

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout.precision(10);
    /*--------------------------------*/
    
    int n,m;cin >> n >> m;
    weight_uf 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