結果
問題 | No.1605 Matrix Shape |
ユーザー | ntuda |
提出日時 | 2021-08-13 22:00:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 647 ms / 2,000 ms |
コード長 | 1,838 bytes |
コンパイル時間 | 347 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 149,696 KB |
最終ジャッジ日時 | 2024-10-03 18:51:50 |
合計ジャッジ時間 | 11,723 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
72,064 KB |
testcase_01 | AC | 52 ms
71,808 KB |
testcase_02 | AC | 39 ms
52,468 KB |
testcase_03 | AC | 51 ms
72,064 KB |
testcase_04 | AC | 39 ms
52,736 KB |
testcase_05 | AC | 53 ms
72,064 KB |
testcase_06 | AC | 52 ms
71,424 KB |
testcase_07 | AC | 38 ms
52,352 KB |
testcase_08 | AC | 53 ms
72,076 KB |
testcase_09 | AC | 53 ms
71,552 KB |
testcase_10 | AC | 53 ms
72,192 KB |
testcase_11 | AC | 50 ms
70,656 KB |
testcase_12 | AC | 51 ms
71,808 KB |
testcase_13 | AC | 52 ms
71,552 KB |
testcase_14 | AC | 51 ms
71,680 KB |
testcase_15 | AC | 50 ms
70,272 KB |
testcase_16 | AC | 49 ms
70,528 KB |
testcase_17 | AC | 48 ms
70,528 KB |
testcase_18 | AC | 49 ms
70,656 KB |
testcase_19 | AC | 635 ms
149,452 KB |
testcase_20 | AC | 562 ms
121,576 KB |
testcase_21 | AC | 574 ms
121,816 KB |
testcase_22 | AC | 481 ms
133,440 KB |
testcase_23 | AC | 647 ms
148,280 KB |
testcase_24 | AC | 646 ms
149,696 KB |
testcase_25 | AC | 291 ms
111,616 KB |
testcase_26 | AC | 294 ms
114,924 KB |
testcase_27 | AC | 294 ms
114,868 KB |
testcase_28 | AC | 250 ms
96,764 KB |
testcase_29 | AC | 291 ms
111,408 KB |
testcase_30 | AC | 549 ms
131,028 KB |
testcase_31 | AC | 563 ms
131,012 KB |
testcase_32 | AC | 453 ms
116,016 KB |
testcase_33 | AC | 379 ms
105,216 KB |
testcase_34 | AC | 273 ms
127,744 KB |
testcase_35 | AC | 516 ms
128,516 KB |
testcase_36 | AC | 360 ms
109,352 KB |
ソースコード
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 n = i uf = UnionFind(n) for hw in HW: h,w = hw uf.union(dic[h],dic[w]) if uf.group_count() != 1: print(0) sys.exit() MAX = 2*10**5+1 H = [[] for _ in range(MAX)] W = [[] for _ in range(MAX)] for hw in HW: h,w = hw H[h].append(w) W[w].append(h) cnt = 0 for i in range(MAX): cnt += abs(len(H[i]) - len(W[i])) if cnt > 2: print(0) sys.exit() if cnt == 0: print(n) else: print(1)