結果

問題 No.1605 Matrix Shape
ユーザー hir355hir355
提出日時 2021-07-16 22:13:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 773 ms / 2,000 ms
コード長 1,542 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 130,416 KB
最終ジャッジ日時 2023-09-20 14:21:46
合計ジャッジ時間 13,867 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
87,400 KB
testcase_01 AC 95 ms
87,208 KB
testcase_02 AC 96 ms
87,308 KB
testcase_03 AC 98 ms
87,224 KB
testcase_04 AC 98 ms
87,216 KB
testcase_05 AC 99 ms
87,156 KB
testcase_06 AC 95 ms
87,200 KB
testcase_07 AC 93 ms
87,220 KB
testcase_08 AC 94 ms
87,200 KB
testcase_09 AC 94 ms
87,128 KB
testcase_10 AC 95 ms
87,304 KB
testcase_11 AC 89 ms
86,220 KB
testcase_12 AC 95 ms
87,036 KB
testcase_13 AC 97 ms
87,308 KB
testcase_14 AC 93 ms
87,212 KB
testcase_15 AC 96 ms
87,028 KB
testcase_16 AC 95 ms
87,164 KB
testcase_17 AC 89 ms
86,156 KB
testcase_18 AC 89 ms
86,168 KB
testcase_19 AC 764 ms
129,400 KB
testcase_20 AC 750 ms
113,652 KB
testcase_21 AC 773 ms
113,744 KB
testcase_22 AC 752 ms
130,396 KB
testcase_23 AC 747 ms
129,432 KB
testcase_24 AC 759 ms
130,416 KB
testcase_25 AC 245 ms
101,632 KB
testcase_26 AC 261 ms
101,644 KB
testcase_27 AC 259 ms
101,436 KB
testcase_28 AC 251 ms
101,720 KB
testcase_29 AC 254 ms
101,672 KB
testcase_30 AC 702 ms
114,776 KB
testcase_31 AC 722 ms
115,348 KB
testcase_32 AC 601 ms
108,036 KB
testcase_33 AC 625 ms
107,920 KB
testcase_34 AC 238 ms
103,872 KB
testcase_35 AC 631 ms
119,960 KB
testcase_36 AC 472 ms
112,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)
 
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
 
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

MAX = 2 * 10 ** 5
n = int(input())
g = [[] for _ in range(MAX + 1)]
inv = [0] * (MAX + 1)
out = [0] * (MAX + 1)
s = set()
uf = UnionFind(MAX + 1)
for i in range(n):
    h, w = map(int, input().split())
    uf.unite(h, w)
    s.add(h)
    s.add(w)
    g[h].append(w)
    inv[w] += 1
    out[h] += 1
f1 = 0
f2 = 0
v = [0] * (MAX + 1)
cnt = 0
for i in range(MAX + 1):
    j = uf.find(i)
    if j in s and v[j] == 0:
        v[j] = 1
        cnt += 1
    if inv[i] + 1 == out[i]:
        f1 += 1
    elif out[i] + 1 == inv[i]:
        f2 += 1
    elif inv[i] == out[i]:
        continue
    else:
        print(0)
        exit(0)
if cnt >= 2:
    print(0)
    exit(0)
if f1 == f2 == 0:
    print(len(s))
elif f1 == f2 == 1:
    print(1)
else:
    print(0)
0