結果
問題 | No.1605 Matrix Shape |
ユーザー |
👑 ![]() |
提出日時 | 2021-06-27 16:43:29 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,888 bytes |
コンパイル時間 | 126 ms |
コンパイル使用メモリ | 82,252 KB |
実行使用メモリ | 101,904 KB |
最終ジャッジ日時 | 2024-07-06 07:54:46 |
合計ジャッジ時間 | 5,890 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 WA * 4 |
ソースコード
"""想定誤解法 1非連結の時 0 を忘れてしまった解法"""import sysfrom sys import stdindef uf_find(n,p):ufl = []while p[n] != n:ufl.append(n)n = p[n]for i in ufl:p[i] = nreturn ndef uf_union(a,b,p,rank):ap = uf_find(a,p)bp = uf_find(b,p)if ap == bp:return Falseelse:if rank[ap] > rank[bp]:p[bp] = apelif rank[ap] < rank[bp]:p[ap] = bpelse:p[bp] = aprank[ap] += 1return TrueN = int(stdin.readline())assert 2 <= N <= 2*(10**5)in_num = {} #相対入次数merge_num = 0 #ufでマージした回数p = [i for i in range(200001)]rank = [0] * (200001)for i in range(N):H,W = map(int,stdin.readline().split())assert 1 <= H <= 2*(10**5)assert 1 <= W <= 2*(10**5)if H not in in_num:in_num[H] = 0if W not in in_num:in_num[W] = 0#相対入次数を変更in_num[H] -= 1in_num[W] += 1#union-findでマージif uf_union(H,W,p,rank):merge_num += 1num_0 = 0 #0の数num_1 = 0 #1の数num_minus_1 = 0 #-1の数num_other = 0 #他の数字の数for i in in_num:if in_num[i] == 0:num_0 += 1elif in_num[i] == 1:num_1 += 1elif in_num[i] == -1:num_minus_1 += 1else:num_other += 1#ここから答えの判定#連結でない場合は、0#if merge_num != len(in_num)-1:# print (0)# sys.exit()#0,-1,1以外があったら0if num_other > 0:print (0)sys.exit()#-1,1が1個づつあったら 1if num_1 == 1 and num_minus_1 == 1:print (1)sys.exit()#全て0なら、登場した数字全てif num_1 == 0 and num_1 == 0 and num_minus_1 == 0:print (num_0)sys.exit()#そうでないなら、0print (0)