結果
| 問題 | No.1605 Matrix Shape |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-10 21:48:02 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 574 ms / 2,000 ms |
| コード長 | 1,347 bytes |
| 記録 | |
| コンパイル時間 | 327 ms |
| コンパイル使用メモリ | 85,892 KB |
| 実行使用メモリ | 110,264 KB |
| 最終ジャッジ日時 | 2026-03-05 04:28:34 |
| 合計ジャッジ時間 | 9,338 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
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 main():
n = int(input())
uf = UnionFind(2*10**5+1)
d = defaultdict(int)
for i in range(n):
h, w = map(int, input().split())
d[h] += 1
d[w] -= 1
uf.union(h, w)
keys = list(d.keys())
values = list(d.values())
nodenum = len(d)
for i in range(nodenum):
if not uf.same(keys[0], keys[i]):
return 0
zerocounter = values.count(0)
if zerocounter == len(values):
return len(values)
elif zerocounter == len(values)-2 and values.count(1) == 1 and values.count(-1) == 1:
return 1
else:
return 0
print(main())