結果

問題 No.1420 国勢調査 (Easy)
ユーザー vwxyzvwxyz
提出日時 2023-05-24 20:42:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 655 ms / 2,000 ms
コード長 2,812 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 11,092 KB
実行使用メモリ 19,116 KB
最終ジャッジ日時 2023-08-25 13:11:23
合計ジャッジ時間 12,451 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,540 KB
testcase_01 AC 17 ms
8,444 KB
testcase_02 AC 323 ms
9,640 KB
testcase_03 AC 325 ms
9,656 KB
testcase_04 AC 323 ms
9,540 KB
testcase_05 AC 317 ms
9,540 KB
testcase_06 AC 323 ms
9,580 KB
testcase_07 AC 99 ms
9,540 KB
testcase_08 AC 212 ms
9,576 KB
testcase_09 AC 117 ms
9,668 KB
testcase_10 AC 221 ms
9,580 KB
testcase_11 AC 61 ms
9,668 KB
testcase_12 AC 56 ms
18,528 KB
testcase_13 AC 239 ms
18,456 KB
testcase_14 AC 66 ms
18,520 KB
testcase_15 AC 241 ms
18,528 KB
testcase_16 AC 241 ms
18,452 KB
testcase_17 AC 240 ms
18,528 KB
testcase_18 AC 238 ms
18,384 KB
testcase_19 AC 243 ms
18,588 KB
testcase_20 AC 240 ms
18,624 KB
testcase_21 AC 244 ms
18,464 KB
testcase_22 AC 652 ms
19,116 KB
testcase_23 AC 653 ms
18,976 KB
testcase_24 AC 653 ms
19,052 KB
testcase_25 AC 655 ms
19,088 KB
testcase_26 AC 655 ms
18,960 KB
testcase_27 AC 214 ms
18,796 KB
testcase_28 AC 207 ms
18,652 KB
testcase_29 AC 125 ms
18,436 KB
testcase_30 AC 218 ms
18,708 KB
testcase_31 AC 203 ms
18,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if self.weighted:
            w=0
            for y in stack[::-1]:
                self.parents[y]=x
                w^=self.weight[y]
                self.weight[y]=w
        else:
            for y in stack[::-1]:
                self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]^self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w^self.weight[x]^self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]^self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {m}" for r,m in linked_components.items())

N,M=map(int,readline().split())
UF=UnionFind(N,weighted=True)
for m in range(M):
    A,B=map(int,readline().split())
    Y=int(readline())
    A-=1;B-=1
    if UF.Union(A,B,Y)==False:
        print(-1)
        exit()
for x in range(N):
    print(UF.Weight(x,UF.Find(x)))
0