結果

問題 No.1605 Matrix Shape
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-07-16 23:30:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,312 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 85,348 KB
最終ジャッジ日時 2023-09-20 16:02:30
合計ジャッジ時間 9,105 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
80,284 KB
testcase_01 AC 79 ms
80,504 KB
testcase_02 AC 85 ms
79,788 KB
testcase_03 WA -
testcase_04 AC 78 ms
79,768 KB
testcase_05 AC 79 ms
80,652 KB
testcase_06 WA -
testcase_07 AC 78 ms
79,864 KB
testcase_08 WA -
testcase_09 AC 80 ms
80,316 KB
testcase_10 AC 79 ms
80,292 KB
testcase_11 AC 77 ms
79,784 KB
testcase_12 WA -
testcase_13 AC 81 ms
80,596 KB
testcase_14 AC 79 ms
80,444 KB
testcase_15 AC 81 ms
80,384 KB
testcase_16 AC 80 ms
80,316 KB
testcase_17 AC 79 ms
79,888 KB
testcase_18 AC 79 ms
79,700 KB
testcase_19 AC 391 ms
84,936 KB
testcase_20 WA -
testcase_21 AC 380 ms
83,664 KB
testcase_22 AC 391 ms
83,732 KB
testcase_23 WA -
testcase_24 AC 386 ms
84,136 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 193 ms
82,220 KB
testcase_28 AC 200 ms
82,132 KB
testcase_29 AC 194 ms
82,344 KB
testcase_30 WA -
testcase_31 AC 354 ms
83,568 KB
testcase_32 WA -
testcase_33 AC 308 ms
83,316 KB
testcase_34 WA -
testcase_35 AC 338 ms
83,844 KB
testcase_36 AC 273 ms
83,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
count = [0]*M
use = [0]*M
uf = Unionfind(M)
for i in range(n):
    h,w = map(int,input().split())
    count[h] += 1
    count[w] -= 1
    use[h] = 1
    use[w] = 1
    uf.union(h,w)
p = 0
m = 0
for i in range(M):
    if use[i]:
        if uf.size(i) != sum(use):
            print(0)
            exit()
        else:
            break
for i in range(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)
0