結果
問題 | No.1605 Matrix Shape |
ユーザー | H3PO4 |
提出日時 | 2024-07-07 11:54:53 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,183 ms / 2,000 ms |
コード長 | 1,727 bytes |
コンパイル時間 | 177 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 37,944 KB |
最終ジャッジ日時 | 2024-07-07 11:55:15 |
合計ジャッジ時間 | 19,937 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 64 ms
23,424 KB |
testcase_01 | AC | 62 ms
23,296 KB |
testcase_02 | AC | 52 ms
23,424 KB |
testcase_03 | AC | 64 ms
23,424 KB |
testcase_04 | AC | 49 ms
23,424 KB |
testcase_05 | AC | 63 ms
23,424 KB |
testcase_06 | AC | 61 ms
23,424 KB |
testcase_07 | AC | 47 ms
23,424 KB |
testcase_08 | AC | 61 ms
23,424 KB |
testcase_09 | AC | 63 ms
23,296 KB |
testcase_10 | AC | 63 ms
23,424 KB |
testcase_11 | AC | 60 ms
23,552 KB |
testcase_12 | AC | 61 ms
23,424 KB |
testcase_13 | AC | 61 ms
23,424 KB |
testcase_14 | AC | 62 ms
23,424 KB |
testcase_15 | AC | 69 ms
23,424 KB |
testcase_16 | AC | 62 ms
23,424 KB |
testcase_17 | AC | 63 ms
23,424 KB |
testcase_18 | AC | 61 ms
23,552 KB |
testcase_19 | AC | 1,141 ms
37,904 KB |
testcase_20 | AC | 994 ms
30,652 KB |
testcase_21 | AC | 977 ms
30,584 KB |
testcase_22 | AC | 1,110 ms
37,944 KB |
testcase_23 | AC | 1,183 ms
37,872 KB |
testcase_24 | AC | 1,178 ms
37,872 KB |
testcase_25 | AC | 814 ms
23,552 KB |
testcase_26 | AC | 798 ms
23,424 KB |
testcase_27 | AC | 818 ms
23,552 KB |
testcase_28 | AC | 793 ms
23,424 KB |
testcase_29 | AC | 811 ms
23,424 KB |
testcase_30 | AC | 971 ms
30,332 KB |
testcase_31 | AC | 992 ms
30,264 KB |
testcase_32 | AC | 837 ms
26,368 KB |
testcase_33 | AC | 847 ms
26,240 KB |
testcase_34 | AC | 724 ms
23,424 KB |
testcase_35 | AC | 832 ms
37,336 KB |
testcase_36 | AC | 559 ms
30,568 KB |
ソースコード
from collections import Counter class UnionFind: __slots__ = ["n", "parent", "height", "size"] def __init__(self, n): self.n = n self.parent = [i for i in range(n)] self.height = [1] * n self.size = [1] * n def find(self, x): if self.parent[x] == x: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.height[x] < self.height[y]: self.parent[x] = y self.size[y] += self.size[x] else: self.parent[y] = x self.size[x] += self.size[y] if self.height[x] == self.height[y]: self.height[x] += 1 def issame(self, x, y): return self.find(x) == self.find(y) def group_size(self, x): return self.size[self.find(x)] def group_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.parent) if i == x] def group_count(self): return len(self.roots()) N = int(input()) MAX = 2 * 10**5 deg = [0] * MAX st = set() uf = UnionFind(MAX) for _ in range(N): H, W = (int(x) - 1 for x in input().split()) deg[H] += 1 deg[W] -= 1 uf.unite(H, W) st.add(H) st.add(W) # 連結か if len(set(uf.find(x) for x in st)) == 1: ct = Counter(deg) if ct[0] == MAX: print(len(st)) elif ct[0] == MAX - 2 and ct[-1] == ct[1] == 1: print(1) else: print(0) else: print(0)