結果

問題 No.1420 国勢調査 (Easy)
コンテスト
ユーザー 👑 希丝缇娜菲贝尔
提出日時 2026-10-06 00:42:12
言語 C++23(gnu拡張gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=gnu++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,200 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,801 ms
コンパイル使用メモリ 287,672 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-06 00:42:24
合計ジャッジ時間 12,080 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 1
other AC * 12 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifdef MAIN 
bool __multi__ = 0;
using INT = long long;

namespace XK {

struct UF {
	int N; 
	V<int> fa, data;
	UF(int n) : N(n) {
		fa.resize(N); data.assign(N, 0);
		rep(i, N) fa[i] = i;
	}
	int root(int x) {
		if(fa[x] == x) return x;
		int f = fa[x];
		int faa = root(fa[x]);
		fa[f] = faa;
		data[x] ^= data[f];
		return faa;
	}
	int merge(int x, int y, int v) {
		int rx = root(x), ry = root(y);
		if(rx == ry) {
			if((data[x] ^ data[y]) != v) return -1;
			return 0;
		}
		fa[rx] = y, data[rx] = v ^ data[x];
		return 0;
	}
};

  void solve() {
  	INT N, M; cin >> N >> M;
  	UF UF(N);


  	rep(i, M) {
  		INT A, B, M; cin >> A >> B >> M; -- A, -- B;
  		if(UF.merge(A, B, M) == -1) {return cout << "-1\n", void();}
  	}
  	rep(i, N) UF.root(i);
  	rep(i, N) cout << UF.data[i] << "\n";
  }
};

#else
#include "cassert"
#include "cmath"
#include "cstdint"
#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "algorithm"
#include "bitset"
#include "chrono"
#include "complex"
#include "deque"
#include "functional"
#include "iostream"
#include "limits"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "sstream"
#include "string"
#include "unordered_map"
#include "unordered_set"
#include "utility"
#include "vector"
#include "array"
using namespace std;
#define int long long
using ll = long long;
using ull = unsigned long long;
const ll INF = 1ll << 60;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
template <class A, class B> bool chmax(A& l, const B& r){ return r > l ? l = r, 1 : 0; }
template <class A, class B> bool chmin(A& l, const B& r){ return r < l ? l = r, 1 : 0; }
#define sz(x) ssize(x)
#define rep(i, a) for(ll i = 0; i < (a); i ++)
#define Rep(i, a, b) for(ll i = (a); i < (b); i ++)
#define rrep(i, a, b) for(ll i = (b); i --> (a); )
#define all(x) begin(x), end(x)
#define fst first
#define snd second
#define pb push_back
template<class T> using V = vector<T>;
template<class T, std::size_t N> using AR = array<T, N>;
#define MAIN
#include __FILE__
signed main() {ios::sync_with_stdio(0);cin.tie(0);fixed(cout).precision(12);int t = 1;if(__multi__) cin >> t;while(t --) XK::solve();}
#endif
0