結果

問題 No.1605 Matrix Shape
ユーザー ntudantuda
提出日時 2021-08-13 22:00:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 659 ms / 2,000 ms
コード長 1,838 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 149,564 KB
最終ジャッジ日時 2024-04-14 17:52:44
合計ジャッジ時間 11,393 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
72,480 KB
testcase_01 AC 51 ms
72,576 KB
testcase_02 AC 36 ms
52,744 KB
testcase_03 AC 50 ms
72,764 KB
testcase_04 AC 37 ms
54,244 KB
testcase_05 AC 51 ms
72,928 KB
testcase_06 AC 51 ms
73,292 KB
testcase_07 AC 36 ms
52,872 KB
testcase_08 AC 52 ms
73,084 KB
testcase_09 AC 51 ms
72,856 KB
testcase_10 AC 52 ms
71,892 KB
testcase_11 AC 48 ms
71,492 KB
testcase_12 AC 51 ms
73,232 KB
testcase_13 AC 52 ms
73,596 KB
testcase_14 AC 51 ms
72,216 KB
testcase_15 AC 48 ms
70,908 KB
testcase_16 AC 49 ms
70,940 KB
testcase_17 AC 49 ms
71,740 KB
testcase_18 AC 49 ms
71,528 KB
testcase_19 AC 659 ms
149,564 KB
testcase_20 AC 566 ms
121,688 KB
testcase_21 AC 568 ms
122,072 KB
testcase_22 AC 478 ms
133,444 KB
testcase_23 AC 646 ms
148,408 KB
testcase_24 AC 645 ms
149,564 KB
testcase_25 AC 294 ms
111,608 KB
testcase_26 AC 295 ms
114,816 KB
testcase_27 AC 295 ms
114,968 KB
testcase_28 AC 247 ms
96,576 KB
testcase_29 AC 296 ms
111,456 KB
testcase_30 AC 574 ms
131,032 KB
testcase_31 AC 577 ms
130,880 KB
testcase_32 AC 480 ms
115,768 KB
testcase_33 AC 381 ms
105,460 KB
testcase_34 AC 268 ms
127,932 KB
testcase_35 AC 522 ms
128,396 KB
testcase_36 AC 373 ms
109,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
N = int(input())
HW = [list(map(int,input().split())) for _ in range(N)]

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):
        return {r: self.members(r) for r in self.roots()}
 
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
 
dic = {}
i = 0
for hw in HW:
    h,w = hw
    if not(h in dic):
        dic[h] = i
        i += 1
    if not(w in dic):
        dic[w] = i
        i += 1
n = i
uf = UnionFind(n)
for hw in HW:
    h,w = hw
    uf.union(dic[h],dic[w])

if uf.group_count() != 1:
    print(0)
    sys.exit()

MAX = 2*10**5+1
H = [[] for _ in range(MAX)]
W = [[] for _ in range(MAX)] 
for hw in HW:
    h,w = hw
    H[h].append(w)
    W[w].append(h)

cnt = 0
for i in range(MAX):
    cnt += abs(len(H[i]) - len(W[i]))
    if cnt > 2:
        print(0)
        sys.exit()
if cnt == 0:
    print(n)
else:
    print(1)
0