結果

問題 No.1605 Matrix Shape
ユーザー 👑 KazunKazun
提出日時 2021-07-16 22:01:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,803 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 127,252 KB
最終ジャッジ日時 2023-09-20 13:55:58
合計ジャッジ時間 8,872 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,260 KB
testcase_01 AC 74 ms
71,268 KB
testcase_02 WA -
testcase_03 AC 73 ms
71,100 KB
testcase_04 WA -
testcase_05 AC 75 ms
71,304 KB
testcase_06 AC 75 ms
71,280 KB
testcase_07 WA -
testcase_08 AC 76 ms
71,284 KB
testcase_09 AC 74 ms
71,420 KB
testcase_10 AC 73 ms
71,328 KB
testcase_11 AC 74 ms
71,144 KB
testcase_12 AC 75 ms
71,280 KB
testcase_13 AC 88 ms
83,652 KB
testcase_14 AC 73 ms
71,252 KB
testcase_15 AC 74 ms
71,168 KB
testcase_16 AC 74 ms
71,336 KB
testcase_17 AC 73 ms
71,208 KB
testcase_18 AC 73 ms
71,308 KB
testcase_19 AC 348 ms
126,676 KB
testcase_20 AC 342 ms
106,156 KB
testcase_21 AC 317 ms
92,180 KB
testcase_22 WA -
testcase_23 AC 343 ms
126,788 KB
testcase_24 AC 300 ms
95,612 KB
testcase_25 AC 239 ms
87,696 KB
testcase_26 AC 240 ms
87,648 KB
testcase_27 AC 242 ms
87,600 KB
testcase_28 WA -
testcase_29 AC 235 ms
87,600 KB
testcase_30 AC 339 ms
106,216 KB
testcase_31 AC 324 ms
91,676 KB
testcase_32 AC 307 ms
96,660 KB
testcase_33 WA -
testcase_34 AC 167 ms
90,432 KB
testcase_35 AC 267 ms
92,608 KB
testcase_36 AC 208 ms
86,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    __slots__=["n","parents","rank"]
    def __init__(self,N):
        """0,1,...,N-1を要素として初期化する.

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.rank[x]<self.rank[y]:
            x,y=y,x

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()
#=================================================
import sys

input=sys.stdin.readline
N=int(input())

M=0
S=[]
for _ in range(N):
    h,w=map(int,input().split())

    M=max(M,h,w)
    S.append((h,w))

U=Union_Find(M+1)
for h,w in S:
    U.union(h,w)

T=set()
for h,_ in S:
    T.add(U.find(h))

if len(T)>=2:
    print(0)

deg=[0]*(M+1)
for h,w in S:
    deg[h]+=1
    deg[w]-=1

M=[d==0 for d in deg]
if all([d==0 for d in deg]):
    K=set()
    for h,w in S:
        K.add(h)
        K.add(w)

    print(len(K))
    exit()

p=q=0
for d in deg:
    if d==1:
        p+=1
    if d==-1:
        q+=1

if p==q==1:
    print(1)
else:
    print(0)
0