結果

問題 No.1605 Matrix Shape
ユーザー hir355hir355
提出日時 2021-07-16 22:07:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,551 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 130,524 KB
最終ジャッジ日時 2023-09-20 14:09:56
合計ジャッジ時間 14,014 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
86,920 KB
testcase_01 AC 91 ms
86,988 KB
testcase_02 AC 92 ms
86,940 KB
testcase_03 AC 91 ms
87,064 KB
testcase_04 AC 92 ms
87,228 KB
testcase_05 AC 90 ms
87,044 KB
testcase_06 AC 89 ms
86,936 KB
testcase_07 AC 91 ms
87,192 KB
testcase_08 AC 92 ms
86,884 KB
testcase_09 AC 93 ms
86,928 KB
testcase_10 AC 94 ms
87,240 KB
testcase_11 AC 86 ms
86,072 KB
testcase_12 AC 92 ms
87,176 KB
testcase_13 AC 96 ms
87,088 KB
testcase_14 AC 92 ms
86,988 KB
testcase_15 AC 92 ms
86,888 KB
testcase_16 AC 93 ms
87,044 KB
testcase_17 AC 86 ms
86,012 KB
testcase_18 AC 88 ms
86,040 KB
testcase_19 AC 751 ms
129,072 KB
testcase_20 AC 762 ms
113,604 KB
testcase_21 AC 783 ms
113,956 KB
testcase_22 AC 766 ms
130,524 KB
testcase_23 AC 760 ms
129,408 KB
testcase_24 AC 754 ms
129,824 KB
testcase_25 AC 241 ms
101,092 KB
testcase_26 AC 245 ms
101,788 KB
testcase_27 AC 246 ms
101,424 KB
testcase_28 WA -
testcase_29 AC 242 ms
101,620 KB
testcase_30 AC 674 ms
114,768 KB
testcase_31 AC 700 ms
114,748 KB
testcase_32 AC 612 ms
107,796 KB
testcase_33 AC 628 ms
108,180 KB
testcase_34 AC 230 ms
103,688 KB
testcase_35 AC 639 ms
119,472 KB
testcase_36 AC 482 ms
112,444 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 uf.size[j] >= 2 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