結果

問題 No.1605 Matrix Shape
ユーザー MineMine
提出日時 2021-09-10 18:32:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,152 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 10,960 KB
実行使用メモリ 28,160 KB
最終ジャッジ日時 2023-09-02 11:05:10
合計ジャッジ時間 17,761 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
9,632 KB
testcase_01 AC 17 ms
9,624 KB
testcase_02 AC 17 ms
9,536 KB
testcase_03 AC 16 ms
9,536 KB
testcase_04 AC 17 ms
9,532 KB
testcase_05 AC 17 ms
9,676 KB
testcase_06 WA -
testcase_07 AC 17 ms
9,632 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 17 ms
9,564 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 889 ms
27,780 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 861 ms
27,676 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 656 ms
9,920 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 742 ms
13,968 KB
testcase_34 AC 463 ms
9,628 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

def main():
    n = int(input())
    uf = UnionFind(2*10**5+1)
    loop = False
    s = set()
    for i in range(n):
        h, w = map(int, input().split())
        s.add(h)
        s.add(w)
        if h != w:
            if uf.same(h, w):
                loop = True
            uf.union(h, w)

    s = list(s)
    pr = uf.find(s[0])
    for i in s:
        if pr != uf.find(i):
            return 0
    if loop:
        return n
    else:
        return 1

print(main())
0