結果

問題 No.1605 Matrix Shape
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-07-16 23:31:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 84,860 KB
最終ジャッジ日時 2023-09-20 16:03:23
合計ジャッジ時間 8,999 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
80,352 KB
testcase_01 AC 79 ms
80,200 KB
testcase_02 AC 76 ms
79,560 KB
testcase_03 AC 77 ms
80,296 KB
testcase_04 AC 77 ms
79,780 KB
testcase_05 AC 79 ms
80,164 KB
testcase_06 AC 80 ms
80,352 KB
testcase_07 AC 77 ms
79,416 KB
testcase_08 AC 79 ms
80,348 KB
testcase_09 AC 79 ms
80,184 KB
testcase_10 AC 79 ms
80,196 KB
testcase_11 AC 79 ms
79,276 KB
testcase_12 AC 79 ms
80,404 KB
testcase_13 AC 81 ms
80,348 KB
testcase_14 AC 78 ms
80,276 KB
testcase_15 AC 79 ms
80,292 KB
testcase_16 AC 79 ms
80,292 KB
testcase_17 AC 79 ms
79,584 KB
testcase_18 AC 80 ms
79,424 KB
testcase_19 AC 391 ms
84,860 KB
testcase_20 AC 377 ms
83,724 KB
testcase_21 AC 377 ms
83,212 KB
testcase_22 AC 390 ms
83,492 KB
testcase_23 AC 391 ms
83,856 KB
testcase_24 AC 391 ms
83,856 KB
testcase_25 AC 193 ms
82,016 KB
testcase_26 AC 201 ms
82,284 KB
testcase_27 AC 200 ms
82,200 KB
testcase_28 AC 194 ms
81,908 KB
testcase_29 AC 196 ms
81,804 KB
testcase_30 AC 359 ms
84,376 KB
testcase_31 AC 357 ms
83,496 KB
testcase_32 AC 315 ms
82,808 KB
testcase_33 AC 315 ms
83,060 KB
testcase_34 AC 187 ms
84,680 KB
testcase_35 AC 343 ms
83,668 KB
testcase_36 AC 276 ms
83,556 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(sum(use))
elif p == m == 1:
    print(1)
else:
    print(0)
0