結果
問題 | No.1420 国勢調査 (Easy) |
ユーザー |
|
提出日時 | 2022-10-16 19:51:08 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 42 ms / 2,000 ms |
コード長 | 1,746 bytes |
コンパイル時間 | 2,078 ms |
コンパイル使用メモリ | 197,668 KB |
最終ジャッジ日時 | 2025-02-08 07:15:01 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for(int i = 0; i < (n); i++)using namespace std;typedef long long ll;namespace algebra {template < class T > class XOR {public:using set = T;static constexpr T op(const T &l, const T &r) { return l ^ r; }static constexpr T id = T(0);static constexpr T inv(const T &x) { return x; }static constexpr T pow(const T &x, const int n) { return n & 1 ? x : 0; }static constexpr bool comm = true;};}template < class abel >class pot_uf {public:using T = typename abel::set;vector<int> data;vector< T > pote;pot_uf(int n) : data(n, -1), pote(n, abel::id) {}int root(int x) {if(data[x] < 0) return x;int r = root(data[x]);pote[x] = abel::op(pote[x], pote[data[x]]);return data[x] = r;}T pot(int x) {root(x);return pote[x];}bool same(int x, int y) {return root(x) == root(y);}T diff(int x, int y) {return abel::op(pot(x), abel::inv(pot(y)));}bool unite(int x, int y, T p) {p = abel::op(p, diff(y, x));x = root(x), y = root(y);if(x == y) return p == abel::id;if(data[x] < data[y]) swap(x, y), p = abel::inv(p);data[y] += data[x];data[x] = y;pote[x] = p;return true;}int size(int x) { return -data[root(x)]; }};int main(){cin.tie(0);ios::sync_with_stdio(0);int N,M; cin >> N >> M;pot_uf< algebra::XOR<int> > uf(N);rep(_,M) {int a,b,y; cin >> a >> b >> y; a--; b--;if(!uf.unite(a, b, y)) {cout << -1 << "\n";return 0;}}rep(i,N) cout << uf.pot(i) << "\n";}