結果

問題 No.1420 国勢調査 (Easy)
ユーザー rogi52
提出日時 2022-10-16 19:52:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,726 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 197,208 KB
最終ジャッジ日時 2025-02-08 07:15:19
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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 {
private:
using T = typename abel::set;
public:
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)]; }
private:
vector<int> data;
vector< T > pote;
};
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";
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0