結果
問題 | No.1605 Matrix Shape |
ユーザー | horitaka1999 |
提出日時 | 2021-07-17 12:46:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 655 ms / 2,000 ms |
コード長 | 2,125 bytes |
コンパイル時間 | 175 ms |
コンパイル使用メモリ | 82,204 KB |
実行使用メモリ | 163,136 KB |
最終ジャッジ日時 | 2024-07-07 00:27:24 |
合計ジャッジ時間 | 8,842 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
63,228 KB |
testcase_01 | AC | 42 ms
62,960 KB |
testcase_02 | AC | 44 ms
63,660 KB |
testcase_03 | AC | 47 ms
63,132 KB |
testcase_04 | AC | 42 ms
63,388 KB |
testcase_05 | AC | 43 ms
64,504 KB |
testcase_06 | AC | 43 ms
63,136 KB |
testcase_07 | AC | 45 ms
63,776 KB |
testcase_08 | AC | 43 ms
63,440 KB |
testcase_09 | AC | 42 ms
63,392 KB |
testcase_10 | AC | 42 ms
64,676 KB |
testcase_11 | AC | 43 ms
63,200 KB |
testcase_12 | AC | 42 ms
64,828 KB |
testcase_13 | AC | 42 ms
63,932 KB |
testcase_14 | AC | 42 ms
63,584 KB |
testcase_15 | AC | 42 ms
63,808 KB |
testcase_16 | AC | 43 ms
64,716 KB |
testcase_17 | AC | 43 ms
63,804 KB |
testcase_18 | AC | 43 ms
63,864 KB |
testcase_19 | AC | 655 ms
152,032 KB |
testcase_20 | AC | 452 ms
120,240 KB |
testcase_21 | AC | 450 ms
115,136 KB |
testcase_22 | AC | 489 ms
138,196 KB |
testcase_23 | AC | 512 ms
163,136 KB |
testcase_24 | AC | 515 ms
162,464 KB |
testcase_25 | AC | 164 ms
79,876 KB |
testcase_26 | AC | 170 ms
80,036 KB |
testcase_27 | AC | 165 ms
79,724 KB |
testcase_28 | AC | 163 ms
79,956 KB |
testcase_29 | AC | 168 ms
79,984 KB |
testcase_30 | AC | 424 ms
122,616 KB |
testcase_31 | AC | 428 ms
122,536 KB |
testcase_32 | AC | 355 ms
101,324 KB |
testcase_33 | AC | 350 ms
101,800 KB |
testcase_34 | AC | 156 ms
79,968 KB |
testcase_35 | AC | 424 ms
140,700 KB |
testcase_36 | AC | 313 ms
114,412 KB |
ソースコード
from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N = int(input()) uf = UnionFind(4*10**5) from collections import defaultdict Euler = defaultdict(int) hw = set() for _ in range(N): h,w = map(int,input().split()) h-=1 w-=1 if h in Euler.keys(): Euler[h] += 1 else: Euler[h] = 1 if w in Euler.keys(): Euler[w] -=1 else: Euler[w] = -1 hw.add(h) hw.add(w) uf.union(h,w) if hw == set(uf.members(list(hw)[0])): cnt0 = 0 cnt1 = 0 cntminus = 0 for key in Euler.keys(): if Euler[key] == 0: cnt0 += 1 if Euler[key] == 1: cnt1 += 1 if Euler[key] == -1: cntminus += 1 V = len(Euler.keys()) if cnt0 == V: print(V) elif cnt0 == V -2 and cnt1 == 1 and cntminus == 1: print(1) else: print(0) else: print(0)