結果

問題 No.1605 Matrix Shape
ユーザー 👑 H20H20
提出日時 2021-07-18 18:27:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,868 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 86,772 KB
実行使用メモリ 85,852 KB
最終ジャッジ日時 2023-09-22 21:12:07
合計ジャッジ時間 12,241 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
81,520 KB
testcase_01 AC 103 ms
81,288 KB
testcase_02 AC 101 ms
80,856 KB
testcase_03 AC 103 ms
81,464 KB
testcase_04 AC 102 ms
80,768 KB
testcase_05 AC 105 ms
81,552 KB
testcase_06 AC 104 ms
81,304 KB
testcase_07 AC 101 ms
80,740 KB
testcase_08 AC 104 ms
81,620 KB
testcase_09 AC 105 ms
81,344 KB
testcase_10 AC 103 ms
81,560 KB
testcase_11 AC 101 ms
80,752 KB
testcase_12 AC 104 ms
81,536 KB
testcase_13 AC 103 ms
81,508 KB
testcase_14 AC 102 ms
81,304 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 100 ms
80,936 KB
testcase_18 AC 99 ms
80,724 KB
testcase_19 AC 427 ms
85,280 KB
testcase_20 AC 414 ms
84,444 KB
testcase_21 AC 416 ms
84,504 KB
testcase_22 AC 428 ms
84,112 KB
testcase_23 AC 450 ms
84,440 KB
testcase_24 AC 425 ms
85,852 KB
testcase_25 AC 214 ms
82,664 KB
testcase_26 AC 222 ms
82,800 KB
testcase_27 AC 212 ms
82,772 KB
testcase_28 AC 214 ms
82,944 KB
testcase_29 AC 214 ms
82,684 KB
testcase_30 AC 383 ms
83,968 KB
testcase_31 AC 378 ms
83,976 KB
testcase_32 AC 345 ms
84,044 KB
testcase_33 AC 350 ms
84,652 KB
testcase_34 AC 206 ms
84,488 KB
testcase_35 WA -
testcase_36 AC 308 ms
84,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
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 members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


N = int(input())
uf = UnionFind(2*10**5+1)
USE = [0]*(2*10**5+1)
L = [0]*(2*10**5+1)
for i in range(N):
    h,w = map(int, input().split())
    uf.union(h,w)
    USE[h],USE[w]=1,1
    L[h]+=1
    L[w]-=1
root = uf.find(h)
useCnt = sum(USE)
if uf.size(root)==useCnt:
    u = 0
    for i in range(len(L)):
        if abs(L[i])>1:
            print(0)
            exit()
        if abs(L[i])==1:
            u+=1
    if u==2:
        print(1)
    else:
        print(useCnt)
else:
    print(0)

0