結果

問題 No.1605 Matrix Shape
ユーザー 👑 KazunKazun
提出日時 2021-07-16 22:01:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 2,804 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,344 KB
実行使用メモリ 127,176 KB
最終ジャッジ日時 2023-09-20 13:56:43
合計ジャッジ時間 8,253 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,540 KB
testcase_01 AC 73 ms
71,584 KB
testcase_02 AC 72 ms
71,504 KB
testcase_03 AC 72 ms
71,364 KB
testcase_04 AC 72 ms
71,048 KB
testcase_05 AC 71 ms
71,548 KB
testcase_06 AC 73 ms
71,476 KB
testcase_07 AC 72 ms
71,260 KB
testcase_08 AC 72 ms
71,300 KB
testcase_09 AC 72 ms
71,364 KB
testcase_10 AC 73 ms
71,444 KB
testcase_11 AC 71 ms
71,356 KB
testcase_12 AC 72 ms
71,472 KB
testcase_13 AC 85 ms
83,632 KB
testcase_14 AC 72 ms
71,472 KB
testcase_15 AC 73 ms
71,500 KB
testcase_16 AC 72 ms
71,368 KB
testcase_17 AC 71 ms
71,632 KB
testcase_18 AC 72 ms
71,168 KB
testcase_19 AC 335 ms
127,176 KB
testcase_20 AC 330 ms
106,448 KB
testcase_21 AC 305 ms
92,124 KB
testcase_22 AC 271 ms
91,272 KB
testcase_23 AC 333 ms
127,176 KB
testcase_24 AC 296 ms
96,128 KB
testcase_25 AC 229 ms
87,920 KB
testcase_26 AC 227 ms
88,048 KB
testcase_27 AC 236 ms
87,904 KB
testcase_28 AC 220 ms
87,840 KB
testcase_29 AC 231 ms
87,896 KB
testcase_30 AC 339 ms
106,376 KB
testcase_31 AC 306 ms
92,088 KB
testcase_32 AC 303 ms
96,832 KB
testcase_33 AC 292 ms
88,816 KB
testcase_34 AC 165 ms
90,556 KB
testcase_35 AC 259 ms
92,888 KB
testcase_36 AC 210 ms
86,040 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:
    exit(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)

    exit(print(len(K)))

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