結果

問題 No.1605 Matrix Shape
ユーザー MineMine
提出日時 2021-09-10 18:46:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,390 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 137,972 KB
最終ジャッジ日時 2023-09-02 11:41:32
合計ジャッジ時間 14,316 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
73,148 KB
testcase_01 AC 100 ms
72,948 KB
testcase_02 AC 104 ms
73,232 KB
testcase_03 WA -
testcase_04 AC 102 ms
72,952 KB
testcase_05 AC 103 ms
73,264 KB
testcase_06 WA -
testcase_07 AC 104 ms
73,284 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 100 ms
73,468 KB
testcase_12 WA -
testcase_13 AC 100 ms
72,956 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 714 ms
137,972 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 646 ms
135,852 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 381 ms
108,164 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 446 ms
103,528 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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)
    loop = False
    counter = defaultdict(int)
    s = set()
    for i in range(n):
        h, w = map(int, input().split())
        s.add(h)
        s.add(w)
        if h == w:
            counter[(h, w)] += 1
            continue
        if counter[(h, w)] != 0:
            return 0
        counter[(h, w)] += 1 
        if uf.same(h, w):
            loop = True
        uf.union(h, w)

    if len(counter) == 1:
        return n
    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