結果
| 問題 |
No.1420 国勢調査 (Easy)
|
| コンテスト | |
| ユーザー |
logx
|
| 提出日時 | 2021-01-29 20:51:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,046 bytes |
| コンパイル時間 | 2,115 ms |
| コンパイル使用メモリ | 198,052 KB |
| 最終ジャッジ日時 | 2025-01-18 08:42:59 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 12 WA * 18 |
ソースコード
#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;
}
logx