結果

問題 No.1605 Matrix Shape
ユーザー Mine
提出日時 2021-09-10 18:32:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,152 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 30,460 KB
最終ジャッジ日時 2024-06-11 17:45:58
合計ジャッジ時間 19,734 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 WA * 24
権限があれば一括ダウンロードができます

ソースコード

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