結果

問題 No.1605 Matrix Shape
ユーザー 👑 tamatotamato
提出日時 2021-07-16 21:38:13
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 1,280 ms
コンパイル使用メモリ 86,820 KB
実行使用メモリ 98,300 KB
最終ジャッジ日時 2023-09-20 13:19:47
合計ジャッジ時間 8,357 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
76,040 KB
testcase_01 AC 78 ms
76,176 KB
testcase_02 AC 77 ms
76,112 KB
testcase_03 AC 74 ms
76,040 KB
testcase_04 AC 76 ms
76,112 KB
testcase_05 AC 76 ms
76,192 KB
testcase_06 AC 77 ms
76,192 KB
testcase_07 AC 79 ms
76,364 KB
testcase_08 AC 77 ms
76,228 KB
testcase_09 AC 79 ms
76,200 KB
testcase_10 AC 76 ms
76,116 KB
testcase_11 AC 79 ms
75,944 KB
testcase_12 AC 77 ms
75,832 KB
testcase_13 AC 77 ms
76,264 KB
testcase_14 AC 76 ms
76,236 KB
testcase_15 AC 77 ms
76,172 KB
testcase_16 AC 77 ms
76,200 KB
testcase_17 AC 78 ms
76,068 KB
testcase_18 AC 76 ms
76,172 KB
testcase_19 AC 338 ms
98,080 KB
testcase_20 AC 297 ms
89,792 KB
testcase_21 AC 303 ms
90,128 KB
testcase_22 AC 278 ms
98,104 KB
testcase_23 AC 325 ms
98,056 KB
testcase_24 AC 324 ms
98,300 KB
testcase_25 AC 167 ms
82,472 KB
testcase_26 AC 175 ms
82,308 KB
testcase_27 AC 171 ms
81,840 KB
testcase_28 AC 164 ms
82,076 KB
testcase_29 AC 169 ms
81,980 KB
testcase_30 AC 303 ms
91,776 KB
testcase_31 AC 308 ms
91,780 KB
testcase_32 AC 264 ms
86,460 KB
testcase_33 AC 259 ms
86,304 KB
testcase_34 AC 116 ms
81,756 KB
testcase_35 AC 265 ms
93,008 KB
testcase_36 AC 215 ms
90,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N = int(input())
    UF = UnionFind(2*10**5+1)
    cnt = [0] * (2*10**5+1)
    HW = set()
    for _ in range(N):
        h, w = map(int, input().split())
        UF.unite(h, w)
        HW.add(h)
        HW.add(w)
        cnt[h] -= 1
        cnt[w] += 1

    M = len(HW)
    for h in HW:
        if UF.size(h) != M:
            print(0)
            exit()

    flg = 0
    for h in HW:
        if cnt[h] == 1:
            flg += 1
        elif cnt[h] > 1:
            flg = -1
    if flg == 0:
        print(M)
    elif flg == 1:
        print(1)
    else:
        print(0)


if __name__ == '__main__':
    main()
0