結果

問題 No.1605 Matrix Shape
ユーザー Mine
提出日時 2021-09-10 21:18:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,316 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,072 KB
実行使用メモリ 103,060 KB
最終ジャッジ日時 2024-06-11 22:04:40
合計ジャッジ時間 11,730 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


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)
    d = defaultdict(int)
    pr = -1
    dead = False
    for i in range(n):
        h, w = map(int, input().split())
        d[h] += 1
        d[w] -= 1
        uf.union(h, w)
        if i == 0:
            pr = uf.find(h)
        elif pr != uf.find(h):
            dead = True

    if dead:
        return 0
    l = list(d.values())
    zerocounter = l.count(0)
    if zerocounter == n:
        return n
    elif zerocounter == n-2 and l.count(1) == 1 and l.count(-1) == 1:
        return 1
    else:
        return 0


print(main())
0