結果
| 問題 |
No.1420 国勢調査 (Easy)
|
| コンテスト | |
| ユーザー |
るさ
|
| 提出日時 | 2021-03-05 21:40:41 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 519 ms / 2,000 ms |
| コード長 | 1,599 bytes |
| コンパイル時間 | 330 ms |
| コンパイル使用メモリ | 81,664 KB |
| 実行使用メモリ | 110,800 KB |
| 最終ジャッジ日時 | 2024-10-07 01:07:53 |
| 合計ジャッジ時間 | 13,213 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
class Union_Find:
def __init__(self, n=0):
self.vertices = n
self.mother = [-1 for i in range(self.vertices)]
self.size_temp = [1 for i in range(self.vertices)]
def root(self, x):
normalize_v = []
while x != -1:
normalize_v.append(x)
y = x
x = self.mother[x]
for i in normalize_v[:-1]:
self.mother[i] = y
return y
def union(self, x, y):
root_x = self.root(x)
root_y = self.root(y)
if root_x != root_y:
self.mother[root_x] = root_y
self.size_temp[root_y] += self.size_temp[root_x]
def find(self, x, y):
if self.root(x) == self.root(y):
return True
else:
return False
def size(self, x):
return self.size_temp[self.root(x)]
n,m = map(int, input().split())
e = [[] for i in range(n)]
uf = Union_Find(n)
for i in range(m):
a,b = map(lambda x:int(x)-1, input().split())
y = int(input())
e[a].append([b,y])
e[b].append([a,y])
uf.union(a, b)
s = []
for i in range(n):
if i == uf.root(i):
s.append(i)
tb = [-1] * n
f = True
for i in s:
tb[i] = 0
while s:
ns = []
for i in s:
for j,k in e[i]:
if tb[j] != -1:
if tb[i] ^ tb[j] ^ k != 0:
f = False
break
else:
tb[j] = tb[i] ^ k
ns.append(j)
if not f:
break
s = ns
if not f:
break
if f:
print(*tb, sep="\n")
else:
print(-1)
るさ