結果

問題 No.1605 Matrix Shape
ユーザー AEnAEn
提出日時 2022-12-21 23:54:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,705 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 114,772 KB
最終ジャッジ日時 2024-04-29 03:34:56
合計ジャッジ時間 14,528 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
88,320 KB
testcase_01 AC 103 ms
88,576 KB
testcase_02 AC 104 ms
88,192 KB
testcase_03 WA -
testcase_04 AC 103 ms
88,576 KB
testcase_05 AC 104 ms
88,576 KB
testcase_06 WA -
testcase_07 AC 104 ms
88,064 KB
testcase_08 WA -
testcase_09 AC 103 ms
88,320 KB
testcase_10 AC 103 ms
88,448 KB
testcase_11 AC 104 ms
88,064 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 103 ms
88,192 KB
testcase_15 AC 103 ms
88,320 KB
testcase_16 AC 103 ms
88,448 KB
testcase_17 AC 103 ms
88,192 KB
testcase_18 AC 104 ms
88,192 KB
testcase_19 AC 715 ms
114,160 KB
testcase_20 WA -
testcase_21 AC 733 ms
108,992 KB
testcase_22 AC 620 ms
113,376 KB
testcase_23 WA -
testcase_24 AC 709 ms
114,024 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 289 ms
99,200 KB
testcase_28 AC 275 ms
99,316 KB
testcase_29 AC 304 ms
99,368 KB
testcase_30 WA -
testcase_31 AC 749 ms
109,672 KB
testcase_32 WA -
testcase_33 AC 644 ms
105,884 KB
testcase_34 WA -
testcase_35 AC 520 ms
109,256 KB
testcase_36 AC 387 ms
106,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

import sys
input = sys.stdin.readline

N = int(input())
d = [[0,0] for _ in range(2*10**5)]
uf = UnionFind(2*10**5)
a = set()
for i in range(N):
    h,w = map(int, input().split())
    h-=1;w-=1
    uf.unite(h,w)
    d[h][0]+=1
    d[w][1]+=1
    a.add(h)
    a.add(w)
    if i==0:f = h
r = uf.root(f)
for i in a:
    if uf.root(i)!=r:
        print(0)
        exit()

c = [0,0,0]
for i in range(N):
    nn = d[i][0]-d[i][1]
    if nn<-1 or nn>1:
        print(0)
        exit()
    c[nn+1] += 1
if c[1]==N:
    print(N)
elif c[0]==1 and c[2]==1:
    print(1)
else:
    print(0)
0