結果
| 問題 |
No.1605 Matrix Shape
|
| コンテスト | |
| ユーザー |
tamato
|
| 提出日時 | 2021-07-16 21:38:13 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 284 ms / 2,000 ms |
| コード長 | 1,614 bytes |
| コンパイル時間 | 792 ms |
| コンパイル使用メモリ | 82,264 KB |
| 実行使用メモリ | 96,552 KB |
| 最終ジャッジ日時 | 2024-07-06 08:36:20 |
| 合計ジャッジ時間 | 6,087 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.buffer.readline
class UnionFind():
def __init__(self, n):
self.n = n
self.root = [-1] * (n + 1)
self.rnk = [0] * (n + 1)
def find_root(self, x):
while self.root[x] >= 0:
x = self.root[x]
return x
def unite(self, x, y):
x = self.find_root(x)
y = self.find_root(y)
if x == y:
return
elif self.rnk[x] > self.rnk[y]:
self.root[x] += self.root[y]
self.root[y] = x
else:
self.root[y] += self.root[x]
self.root[x] = y
if self.rnk[x] == self.rnk[y]:
self.rnk[y] += 1
def isSameGroup(self, x, y):
return self.find_root(x) == self.find_root(y)
def size(self, x):
return -self.root[self.find_root(x)]
N = int(input())
UF = UnionFind(2*10**5+1)
cnt = [0] * (2*10**5+1)
HW = set()
for _ in range(N):
h, w = map(int, input().split())
UF.unite(h, w)
HW.add(h)
HW.add(w)
cnt[h] -= 1
cnt[w] += 1
M = len(HW)
for h in HW:
if UF.size(h) != M:
print(0)
exit()
flg = 0
for h in HW:
if cnt[h] == 1:
flg += 1
elif cnt[h] > 1:
flg = -1
if flg == 0:
print(M)
elif flg == 1:
print(1)
else:
print(0)
if __name__ == '__main__':
main()
tamato