結果
問題 | No.1605 Matrix Shape |
ユーザー |
|
提出日時 | 2021-07-16 23:31:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 307 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 521 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 83,792 KB |
最終ジャッジ日時 | 2024-07-06 11:09:07 |
合計ジャッジ時間 | 6,755 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
class Unionfind:def __init__(self,n):self.uf = [-1]*ndef find(self,x):if self.uf[x] < 0:return xelse: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 Falseif self.uf[x] > self.uf[y]:x,y = y,xself.uf[x] += self.uf[y]self.uf[y] = xreturn Truedef size(self,x):x = self.find(x)return -self.uf[x]n = int(input())M = 2*10**5+5count = [0]*Muse = [0]*Muf = Unionfind(M)for i in range(n):h,w = map(int,input().split())count[h] += 1count[w] -= 1use[h] = 1use[w] = 1uf.union(h,w)p = 0m = 0for i in range(M):if use[i]:if uf.size(i) != sum(use):print(0)exit()else:breakfor i in range(M):if use[i]:if count[i] == 0:continueif abs(count[i]) != 1:print(0)exit()if count[i] == 1:p += 1else:m += 1if p == m == 0:print(sum(use))elif p == m == 1:print(1)else:print(0)