結果

問題 No.1605 Matrix Shape
ユーザー MineMine
提出日時 2021-09-10 21:18:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,316 bytes
コンパイル時間 845 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 106,152 KB
最終ジャッジ日時 2023-09-02 15:31:50
合計ジャッジ時間 13,422 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
73,232 KB
testcase_01 AC 87 ms
73,364 KB
testcase_02 AC 86 ms
73,368 KB
testcase_03 WA -
testcase_04 AC 93 ms
72,948 KB
testcase_05 AC 90 ms
73,032 KB
testcase_06 WA -
testcase_07 AC 87 ms
73,172 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 89 ms
73,232 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 94 ms
73,164 KB
testcase_16 AC 94 ms
73,344 KB
testcase_17 AC 92 ms
73,368 KB
testcase_18 AC 87 ms
73,404 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 574 ms
106,152 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 216 ms
79,548 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 465 ms
88,496 KB
testcase_34 WA -
testcase_35 AC 507 ms
99,084 KB
testcase_36 AC 405 ms
90,476 KB
権限があれば一括ダウンロードができます

ソースコード

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