結果
| 問題 |
No.1605 Matrix Shape
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-16 23:27:42 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,385 bytes |
| コンパイル時間 | 162 ms |
| コンパイル使用メモリ | 81,928 KB |
| 実行使用メモリ | 114,852 KB |
| 最終ジャッジ日時 | 2024-07-06 11:04:59 |
| 合計ジャッジ時間 | 7,730 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 12 WA * 22 |
ソースコード
class Unionfind:
def __init__(self,n):
self.uf = [-1]*n
def find(self,x):
if self.uf[x] < 0:
return x
else:
self.uf[x] = self.find(self.uf[x])
return self.uf[x]
def same(self,x,y):
return self.find(x) == self.find(y)
def union(self,x,y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if self.uf[x] > self.uf[y]:
x,y = y,x
self.uf[x] += self.uf[y]
self.uf[y] = x
return True
def size(self,x):
x = self.find(x)
return -self.uf[x]
n = int(input())
M = 2*10**5+5
e = [[] for i in range(2*M)]
count = [0]*(2*M)
use = [0]*(2*M)
uf = Unionfind(2*M)
for i in range(n):
h,w = map(int,input().split())
w += M
e[h].append(w)
count[h] += 1
count[w] -= 1
use[h] = 1
use[w] = 1
uf.union(h,w)
p = 0
m = 0
for i in range(2*M):
if use[i]:
if uf.size(i) != sum(use):
print(0)
exit()
else:
break
for i in range(2*M):
if use[i]:
if count[i] == 0:
continue
if abs(count[i]) != 1:
print(0)
exit()
if count[i] == 1:
p += 1
else:
m += 1
if p == m == 0:
print(n)
elif p == m == 1:
print(1)
else:
print(0)