結果
問題 | No.1605 Matrix Shape |
ユーザー | ntuda |
提出日時 | 2021-08-13 21:40:42 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,770 bytes |
コンパイル時間 | 239 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 172,116 KB |
最終ジャッジ日時 | 2024-10-03 17:59:25 |
合計ジャッジ時間 | 11,760 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,224 KB |
testcase_01 | AC | 40 ms
52,864 KB |
testcase_02 | AC | 37 ms
52,864 KB |
testcase_03 | AC | 39 ms
52,608 KB |
testcase_04 | AC | 36 ms
52,480 KB |
testcase_05 | AC | 39 ms
52,804 KB |
testcase_06 | AC | 38 ms
52,864 KB |
testcase_07 | AC | 38 ms
52,864 KB |
testcase_08 | AC | 38 ms
52,480 KB |
testcase_09 | AC | 37 ms
52,736 KB |
testcase_10 | AC | 36 ms
52,736 KB |
testcase_11 | WA | - |
testcase_12 | AC | 37 ms
52,864 KB |
testcase_13 | AC | 38 ms
52,352 KB |
testcase_14 | AC | 37 ms
52,608 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 608 ms
171,716 KB |
testcase_20 | AC | 552 ms
132,572 KB |
testcase_21 | AC | 548 ms
132,316 KB |
testcase_22 | AC | 498 ms
133,504 KB |
testcase_23 | AC | 630 ms
171,844 KB |
testcase_24 | AC | 584 ms
172,116 KB |
testcase_25 | AC | 261 ms
96,756 KB |
testcase_26 | AC | 261 ms
97,140 KB |
testcase_27 | AC | 278 ms
96,828 KB |
testcase_28 | AC | 270 ms
96,640 KB |
testcase_29 | AC | 280 ms
96,668 KB |
testcase_30 | AC | 529 ms
121,816 KB |
testcase_31 | AC | 518 ms
132,940 KB |
testcase_32 | AC | 455 ms
115,756 KB |
testcase_33 | AC | 391 ms
105,316 KB |
testcase_34 | AC | 259 ms
96,384 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
ソースコード
import sys N = int(input()) HW = [list(map(int,input().split())) for _ in range(N)] 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): return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) dic = {} i = 0 for hw in HW: h,w = hw if not(h in dic): dic[h] = i i += 1 if not(w in dic): dic[w] = i i += 1 uf = UnionFind(i) for hw in HW: h,w = hw uf.union(dic[h],dic[w]) if uf.group_count() != 1: print(0) sys.exit() dic1 = {} dic2 = {} for hw in HW: h,w = hw if h in dic1: dic1[h] += 1 else: dic1[h] = 1 if w in dic2: dic2[w] += 1 else: dic2[w] = 1 if dic1 == dic2: print(len(dic1)) else: print(1)