結果

問題 No.1420 国勢調査 (Easy)
ユーザー logxlogx
提出日時 2021-01-29 23:15:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 873 ms / 2,000 ms
コード長 1,018 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 13,132 KB
最終ジャッジ日時 2023-09-09 17:01:27
合計ジャッジ時間 19,727 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,320 KB
testcase_01 AC 16 ms
8,240 KB
testcase_02 AC 627 ms
8,844 KB
testcase_03 AC 632 ms
8,908 KB
testcase_04 AC 637 ms
8,764 KB
testcase_05 AC 620 ms
8,780 KB
testcase_06 AC 634 ms
8,940 KB
testcase_07 AC 613 ms
8,724 KB
testcase_08 AC 614 ms
8,708 KB
testcase_09 AC 613 ms
8,824 KB
testcase_10 AC 614 ms
8,832 KB
testcase_11 AC 620 ms
8,700 KB
testcase_12 AC 80 ms
10,528 KB
testcase_13 AC 195 ms
10,748 KB
testcase_14 AC 79 ms
10,344 KB
testcase_15 AC 196 ms
10,868 KB
testcase_16 AC 197 ms
10,772 KB
testcase_17 AC 195 ms
10,780 KB
testcase_18 AC 193 ms
10,792 KB
testcase_19 AC 196 ms
10,708 KB
testcase_20 AC 197 ms
10,776 KB
testcase_21 AC 198 ms
10,688 KB
testcase_22 AC 868 ms
13,092 KB
testcase_23 AC 873 ms
13,068 KB
testcase_24 AC 868 ms
13,000 KB
testcase_25 AC 871 ms
13,120 KB
testcase_26 AC 865 ms
13,068 KB
testcase_27 AC 705 ms
12,932 KB
testcase_28 AC 700 ms
13,072 KB
testcase_29 AC 704 ms
13,132 KB
testcase_30 AC 691 ms
13,096 KB
testcase_31 AC 703 ms
12,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m=map(int,input().split())

par=[-1] * n
diff=[0] * n
def find(x):
        if par[x]<0:
                return x
        r=find(par[x])
        diff[x]^=diff[par[x]]
        par[x]=r
        return r
def weight(x):
        find(x)
        return diff[x]
def diff_query(x,y):
        return weight(x)^weight(y)
def same(x,y):
        return find(x)==find(y)
def unite(x,y,w):
        if(same(x,y)):
                if diff_query(x,y)!=w:
                        return False
                else:
                        return True
        w=(w^weight(x))^weight(y)
        x=find(x)
        y=find(y)
        if par[x]>par[y]:
                x,y = y,x
        t=par[x]+par[y]
        par[x]=t
        par[y]=x
        diff[y]=w
        return True


ok=True
for _ in range(m):
        a,b=map(int,input().split())
        y=int(input())
        if unite(a-1,b-1,y)==False:
                ok=False
if ok==False:
        print(-1)
        exit(0)

for i in range(n):
        r=find(i)
        print(diff_query(i,r))
0