結果

問題 No.1605 Matrix Shape
ユーザー horitaka1999horitaka1999
提出日時 2021-07-17 12:46:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 763 ms / 2,000 ms
コード長 2,125 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 86,796 KB
実行使用メモリ 164,860 KB
最終ジャッジ日時 2023-09-21 06:01:16
合計ジャッジ時間 13,514 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
79,860 KB
testcase_01 AC 105 ms
79,472 KB
testcase_02 AC 106 ms
79,832 KB
testcase_03 AC 105 ms
79,716 KB
testcase_04 AC 107 ms
79,824 KB
testcase_05 AC 106 ms
79,680 KB
testcase_06 AC 105 ms
79,860 KB
testcase_07 AC 103 ms
79,784 KB
testcase_08 AC 105 ms
79,568 KB
testcase_09 AC 105 ms
79,596 KB
testcase_10 AC 103 ms
79,572 KB
testcase_11 AC 104 ms
79,680 KB
testcase_12 AC 106 ms
79,640 KB
testcase_13 AC 104 ms
79,472 KB
testcase_14 AC 105 ms
79,588 KB
testcase_15 AC 103 ms
79,840 KB
testcase_16 AC 102 ms
79,640 KB
testcase_17 AC 102 ms
79,724 KB
testcase_18 AC 101 ms
79,860 KB
testcase_19 AC 763 ms
163,820 KB
testcase_20 AC 721 ms
125,840 KB
testcase_21 AC 641 ms
122,072 KB
testcase_22 AC 657 ms
140,120 KB
testcase_23 AC 696 ms
164,860 KB
testcase_24 AC 710 ms
164,332 KB
testcase_25 AC 239 ms
81,216 KB
testcase_26 AC 247 ms
80,964 KB
testcase_27 AC 239 ms
81,028 KB
testcase_28 AC 240 ms
80,940 KB
testcase_29 AC 238 ms
80,944 KB
testcase_30 AC 627 ms
119,208 KB
testcase_31 AC 623 ms
118,704 KB
testcase_32 AC 509 ms
102,856 KB
testcase_33 AC 510 ms
102,456 KB
testcase_34 AC 223 ms
80,784 KB
testcase_35 AC 576 ms
140,504 KB
testcase_36 AC 446 ms
116,648 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 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(4*10**5)
from collections import defaultdict
Euler = defaultdict(int)
hw = set()
for _ in range(N):
    h,w = map(int,input().split())
    h-=1
    w-=1
    if h in Euler.keys():
        Euler[h] += 1
    else:
        Euler[h] = 1
    if w in Euler.keys():
        Euler[w] -=1
    else:
        Euler[w] = -1
    hw.add(h)
    hw.add(w)
    uf.union(h,w)
if hw == set(uf.members(list(hw)[0])):
    cnt0 = 0
    cnt1 = 0
    cntminus = 0
    for key in Euler.keys():
        if Euler[key] == 0:
            cnt0 += 1
        if Euler[key] == 1:
            cnt1 += 1
        if Euler[key] == -1:
            cntminus += 1
    V = len(Euler.keys())
    if cnt0 == V:
        print(V)
    elif cnt0 == V -2 and cnt1 == 1 and cntminus == 1:
        print(1)
    else:
        print(0)
else:
    print(0)
0