結果
問題 | No.1605 Matrix Shape |
ユーザー | tassei903 |
提出日時 | 2021-07-16 22:06:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 326 ms / 2,000 ms |
コード長 | 2,006 bytes |
コンパイル時間 | 216 ms |
コンパイル使用メモリ | 82,280 KB |
実行使用メモリ | 123,120 KB |
最終ジャッジ日時 | 2024-07-06 09:20:20 |
合計ジャッジ時間 | 6,933 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
55,552 KB |
testcase_01 | AC | 43 ms
54,800 KB |
testcase_02 | AC | 37 ms
54,040 KB |
testcase_03 | AC | 37 ms
54,976 KB |
testcase_04 | AC | 36 ms
54,200 KB |
testcase_05 | AC | 36 ms
55,640 KB |
testcase_06 | AC | 37 ms
55,812 KB |
testcase_07 | AC | 36 ms
55,376 KB |
testcase_08 | AC | 36 ms
56,388 KB |
testcase_09 | AC | 34 ms
54,264 KB |
testcase_10 | AC | 35 ms
55,328 KB |
testcase_11 | AC | 36 ms
55,040 KB |
testcase_12 | AC | 38 ms
55,968 KB |
testcase_13 | AC | 45 ms
64,672 KB |
testcase_14 | AC | 37 ms
54,260 KB |
testcase_15 | AC | 35 ms
55,336 KB |
testcase_16 | AC | 37 ms
54,120 KB |
testcase_17 | AC | 36 ms
55,072 KB |
testcase_18 | AC | 36 ms
54,520 KB |
testcase_19 | AC | 326 ms
123,120 KB |
testcase_20 | AC | 286 ms
108,980 KB |
testcase_21 | AC | 286 ms
109,060 KB |
testcase_22 | AC | 311 ms
122,964 KB |
testcase_23 | AC | 307 ms
120,744 KB |
testcase_24 | AC | 302 ms
121,020 KB |
testcase_25 | AC | 141 ms
100,716 KB |
testcase_26 | AC | 145 ms
101,352 KB |
testcase_27 | AC | 139 ms
100,940 KB |
testcase_28 | AC | 132 ms
101,036 KB |
testcase_29 | AC | 138 ms
100,916 KB |
testcase_30 | AC | 260 ms
106,336 KB |
testcase_31 | AC | 266 ms
106,108 KB |
testcase_32 | AC | 236 ms
102,804 KB |
testcase_33 | AC | 235 ms
103,016 KB |
testcase_34 | AC | 129 ms
100,944 KB |
testcase_35 | AC | 276 ms
108,004 KB |
testcase_36 | AC | 225 ms
95,088 KB |
ソースコード
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) sys.setrecursionlimit(10**7) yes = lambda :print("yes");Yes = lambda :print("Yes") no = lambda :print("no");No = lambda :print("No") ################################################# 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 = ni() h = [] w = [] for i in range(n): a,b = na() a-=1;b-=1 h.append(a) w.append(b) c = set() D = max(h+w)+1 d = [0]*D uf = UnionFind(D) for i in range(n): c.add(h[i]) c.add(w[i]) uf.union(h[i],w[i]) d[h[i]]+=1;d[w[i]]-=1; if uf.size(h[0])!=len(c): print(0) exit() z = sum([abs(i)for i in d]) if z>2: print(0) elif z==2: print(1) else: print(len(c))