結果

問題 No.1420 国勢調査 (Easy)
ユーザー るさるさ
提出日時 2021-03-05 21:40:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 568 ms / 2,000 ms
コード長 1,599 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 110,672 KB
最終ジャッジ日時 2024-04-16 09:06:04
合計ジャッジ時間 13,995 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 39 ms
52,352 KB
testcase_02 AC 410 ms
98,716 KB
testcase_03 AC 411 ms
99,048 KB
testcase_04 AC 411 ms
98,764 KB
testcase_05 AC 420 ms
99,040 KB
testcase_06 AC 402 ms
98,444 KB
testcase_07 AC 380 ms
98,688 KB
testcase_08 AC 382 ms
98,860 KB
testcase_09 AC 377 ms
98,600 KB
testcase_10 AC 374 ms
98,980 KB
testcase_11 AC 393 ms
98,872 KB
testcase_12 AC 173 ms
89,052 KB
testcase_13 AC 204 ms
91,100 KB
testcase_14 AC 176 ms
88,768 KB
testcase_15 AC 211 ms
90,580 KB
testcase_16 AC 206 ms
90,716 KB
testcase_17 AC 205 ms
91,092 KB
testcase_18 AC 203 ms
91,076 KB
testcase_19 AC 211 ms
91,244 KB
testcase_20 AC 213 ms
90,596 KB
testcase_21 AC 209 ms
90,736 KB
testcase_22 AC 560 ms
110,296 KB
testcase_23 AC 553 ms
110,156 KB
testcase_24 AC 565 ms
110,032 KB
testcase_25 AC 568 ms
110,672 KB
testcase_26 AC 563 ms
110,420 KB
testcase_27 AC 480 ms
105,956 KB
testcase_28 AC 467 ms
106,568 KB
testcase_29 AC 487 ms
106,316 KB
testcase_30 AC 474 ms
106,064 KB
testcase_31 AC 466 ms
106,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0