結果

問題 No.1605 Matrix Shape
ユーザー KudeKude
提出日時 2021-07-16 21:54:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 825 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 148,176 KB
最終ジャッジ日時 2023-09-20 13:45:07
合計ジャッジ時間 17,111 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
89,604 KB
testcase_01 AC 195 ms
89,620 KB
testcase_02 AC 190 ms
89,524 KB
testcase_03 AC 197 ms
89,584 KB
testcase_04 AC 192 ms
89,372 KB
testcase_05 AC 192 ms
89,620 KB
testcase_06 AC 195 ms
89,508 KB
testcase_07 AC 193 ms
89,520 KB
testcase_08 AC 192 ms
89,612 KB
testcase_09 AC 196 ms
89,420 KB
testcase_10 AC 199 ms
89,620 KB
testcase_11 AC 194 ms
89,348 KB
testcase_12 AC 200 ms
89,796 KB
testcase_13 AC 207 ms
89,528 KB
testcase_14 AC 206 ms
89,700 KB
testcase_15 AC 206 ms
89,528 KB
testcase_16 AC 205 ms
89,524 KB
testcase_17 AC 200 ms
89,524 KB
testcase_18 AC 199 ms
89,400 KB
testcase_19 AC 825 ms
145,700 KB
testcase_20 AC 731 ms
128,424 KB
testcase_21 AC 744 ms
128,552 KB
testcase_22 AC 640 ms
126,300 KB
testcase_23 AC 788 ms
146,888 KB
testcase_24 AC 782 ms
148,176 KB
testcase_25 AC 457 ms
113,248 KB
testcase_26 AC 463 ms
112,708 KB
testcase_27 AC 461 ms
112,968 KB
testcase_28 AC 450 ms
112,824 KB
testcase_29 AC 457 ms
113,272 KB
testcase_30 AC 779 ms
129,640 KB
testcase_31 AC 775 ms
130,140 KB
testcase_32 AC 598 ms
123,260 KB
testcase_33 AC 581 ms
123,424 KB
testcase_34 AC 454 ms
125,720 KB
testcase_35 AC 542 ms
139,920 KB
testcase_36 AC 396 ms
123,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import types

_atcoder_code = """
# Python port of AtCoder Library.

__version__ = '0.0.1'
"""

atcoder = types.ModuleType('atcoder')
exec(_atcoder_code, atcoder.__dict__)

_atcoder_dsu_code = """
import typing


class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))
"""

atcoder.dsu = types.ModuleType('atcoder.dsu')
exec(_atcoder_dsu_code, atcoder.dsu.__dict__)
DSU = atcoder.dsu.DSU

# from atcoder.dsu import DSU
m = int(input())
n = 2 * 10 ** 5
deg = [0] * n
deg = [0] * n
to = [[] for _ in range(n)]
hw = [tuple(x - 1 for x in map(int, input().split())) for _ in range(m)]
for h, w in hw:
    to[h].append(w)
    deg[h] += 1
    deg[w] -= 1

d = DSU(n)
for i in range(n):
    for j in to[i]:
        d.merge(i, j)
x = hw[0][0]
s = set()
for h, w in hw: 
    if not (d.same(h, x) and d.same(w, x)):
        print(0)
        exit()
    s.add(h)
    s.add(w)

if not any(deg):
    print(len(s))
else:
    cnt0 = cnt1 = 0
    x = y = -1
    for i, d in enumerate(deg):
        if d == 0:
            continue
        elif d == 1:
            cnt0 += 1
        elif d == -1:
            cnt1 += 1
        else:
            cnt0 = 55
            break
    if cnt0 == cnt1 == 1:
        print(1)
    else:
        print(0)
0