結果
| 問題 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 WA * 7 |
ソースコード
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)
ntuda