結果

問題 No.1605 Matrix Shape
ユーザー ntudantuda
提出日時 2021-08-13 21:40:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,770 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 171,988 KB
最終ジャッジ日時 2024-04-14 17:16:16
合計ジャッジ時間 10,641 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,856 KB
testcase_01 AC 41 ms
54,112 KB
testcase_02 AC 38 ms
52,532 KB
testcase_03 AC 36 ms
53,444 KB
testcase_04 AC 37 ms
53,804 KB
testcase_05 AC 37 ms
52,988 KB
testcase_06 AC 38 ms
52,468 KB
testcase_07 AC 36 ms
52,896 KB
testcase_08 AC 38 ms
53,164 KB
testcase_09 AC 37 ms
53,616 KB
testcase_10 AC 37 ms
53,824 KB
testcase_11 WA -
testcase_12 AC 37 ms
53,480 KB
testcase_13 AC 37 ms
53,852 KB
testcase_14 AC 37 ms
52,396 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 588 ms
171,988 KB
testcase_20 AC 535 ms
132,436 KB
testcase_21 AC 530 ms
132,312 KB
testcase_22 AC 474 ms
133,620 KB
testcase_23 AC 570 ms
171,568 KB
testcase_24 AC 545 ms
171,876 KB
testcase_25 AC 257 ms
96,744 KB
testcase_26 AC 259 ms
96,808 KB
testcase_27 AC 259 ms
96,760 KB
testcase_28 AC 247 ms
96,976 KB
testcase_29 AC 256 ms
96,624 KB
testcase_30 AC 522 ms
121,812 KB
testcase_31 AC 499 ms
132,888 KB
testcase_32 AC 434 ms
115,252 KB
testcase_33 AC 374 ms
105,524 KB
testcase_34 AC 235 ms
96,228 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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
uf = UnionFind(i)
for hw in HW:
    h,w = hw
    uf.union(dic[h],dic[w])

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

dic1 = {}
dic2 = {}
for hw in HW:
    h,w = hw
    if h in dic1:
        dic1[h] += 1
    else:
        dic1[h] = 1
    if w in dic2:
        dic2[w] += 1
    else:
        dic2[w] = 1
if dic1 == dic2:
    print(len(dic1))
else:
    print(1)
0