結果
問題 | No.1605 Matrix Shape |
ユーザー | 👑 rin204 |
提出日時 | 2021-07-16 22:25:33 |
言語 | PyPy3 (7.3.13) |
結果 |
AC
|
実行時間 | 601 ms / 2,000 ms |
コード長 | 2,217 bytes |
コンパイル時間 | 268 ms |
コンパイル使用メモリ | 87,176 KB |
実行使用メモリ | 108,908 KB |
最終ジャッジ日時 | 2023-09-20 14:40:26 |
合計ジャッジ時間 | 11,494 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 87 ms
82,068 KB |
testcase_01 | AC | 90 ms
82,192 KB |
testcase_02 | AC | 83 ms
81,292 KB |
testcase_03 | AC | 88 ms
82,452 KB |
testcase_04 | AC | 81 ms
81,420 KB |
testcase_05 | AC | 90 ms
82,212 KB |
testcase_06 | AC | 87 ms
82,188 KB |
testcase_07 | AC | 82 ms
81,368 KB |
testcase_08 | AC | 88 ms
82,212 KB |
testcase_09 | AC | 87 ms
82,244 KB |
testcase_10 | AC | 85 ms
82,072 KB |
testcase_11 | AC | 84 ms
81,980 KB |
testcase_12 | AC | 87 ms
82,064 KB |
testcase_13 | AC | 90 ms
82,164 KB |
testcase_14 | AC | 87 ms
82,052 KB |
testcase_15 | AC | 84 ms
81,944 KB |
testcase_16 | AC | 83 ms
81,964 KB |
testcase_17 | AC | 83 ms
81,944 KB |
testcase_18 | AC | 83 ms
82,216 KB |
testcase_19 | AC | 578 ms
108,908 KB |
testcase_20 | AC | 585 ms
100,752 KB |
testcase_21 | AC | 601 ms
100,924 KB |
testcase_22 | AC | 552 ms
108,092 KB |
testcase_23 | AC | 596 ms
108,052 KB |
testcase_24 | AC | 579 ms
107,796 KB |
testcase_25 | AC | 233 ms
96,780 KB |
testcase_26 | AC | 236 ms
96,720 KB |
testcase_27 | AC | 231 ms
96,320 KB |
testcase_28 | AC | 224 ms
96,380 KB |
testcase_29 | AC | 231 ms
97,012 KB |
testcase_30 | AC | 549 ms
101,024 KB |
testcase_31 | AC | 564 ms
101,540 KB |
testcase_32 | AC | 505 ms
97,476 KB |
testcase_33 | AC | 472 ms
97,964 KB |
testcase_34 | AC | 216 ms
98,000 KB |
testcase_35 | AC | 471 ms
105,256 KB |
testcase_36 | AC | 371 ms
99,652 KB |
ソースコード
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n self.group = 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 self.group -= 1 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 self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) n = int(input()) max_ = 2 * 10 ** 5 UF = UnionFind(max_) edges = [[] for _ in range(max_)] in_cnt = [0] * max_ out_cnt = [0] * max_ for _ in range(n): h, w = map(int, input().split()) h -= 1 w -= 1 UF.union(h, w) edges[h].append(w) in_cnt[h] += 1 out_cnt[w] += 1 for i, o in enumerate(out_cnt): if o != 0: break s = i for j, o in enumerate(out_cnt): if o != 0: if not UF.same(s, j): print(0) exit() s = -1 g = -1 for j, (i, o) in enumerate(zip(in_cnt, out_cnt)): if i == o: continue if i == o + 1: if s == -1: s = j else: print(0) exit() elif i + 1 == o: if g == -1: g = j else: print(0) exit() else: print(0) exit() if s == g == -1: ans = 0 for e in edges: if e: ans += 1 print(ans) else: print(1)