結果

問題 No.1420 国勢調査 (Easy)
ユーザー gorugo30gorugo30
提出日時 2021-03-05 22:11:12
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,690 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 97,960 KB
最終ジャッジ日時 2024-04-16 09:43:54
合計ジャッジ時間 4,391 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
58,496 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, N):
        self.par = [-1] * N
        self.size = [1] * N
    def root(self, x):
        while self.par[x] >= 0:
            x = self.par[x]
        return x
    def union(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return
        if -self.par[rx] == -self.par[ry]:
            self.par[ry] -= 1
            self.par[rx] = ry
            self.size[ry] += self.size[rx]
            return
        if -self.par[rx] > -self.par[rx]:
            rx, ry = ry, rx
        self.par[rx] = ry
        self.size[ry] += self.size[rx]
    def find(self, x, y):
        return self.root(x) == self.root(y)
    def getsize(self, x):
        return self.size[self.root(x)]

N, M = map(int, input().split())
ufts = [UnionFind(2 * N) for i in range(30)]
for hito in range(M):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    Y = int(input())
    for i in range(30):
        if Y & 1:
            if ufts[i].find(A, B):
                print(-1)
                exit()
            ufts[i].union(A, B + N)
            ufts[i].union(A + N, B)
        else:
            if ufts[i].find(A + N, B):
                print(-1)
                exit()
            ufts[i].union(A, B)
            ufts[i].union(B, A)
        Y >>= 1
pow2 = 1
X = [0] * N
for k in range(30):
    x = [0] * N
    for i in range(N):
        if ufts[k].getsize(i) == 1:
            continue
        r = ufts[k].root(i)
        if r < N:
            x[i] = x[r]
            X[i] += pow2 * (x[r])
        else:
            r -= N
            x[i] = 1 ^ x[r]
            X[i] += pow2 * (1 ^ x[r])
    pow2 *= 2
print(*X, sep = "\n")
0