結果

問題 No.1605 Matrix Shape
ユーザー tonyu0tonyu0
提出日時 2022-09-21 13:27:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 775 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 120,392 KB
最終ジャッジ日時 2024-12-22 04:03:48
合計ジャッジ時間 12,462 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())

M=200001
par=[-1]*M
def root(a):
    if par[a]<0:
        return a
    return root(par[a])
def same(a, b):
    return root(a)==root(b)
def merge(a,b):
    if same(a,b):return
    a=root(a)
    b=root(b)
    if par[a]>par[b]:a,b=b,a
    par[a]+=par[b]
    par[b]=a
def sz(a):
    return -par[root(a)]

a=[list(map(lambda x:int(x)-1, input().split())) for i in range(n)]
deg = [0]*M
V=set()
for x,y in a:
    merge(x,y)
    V.add(x)
    V.add(y)
    deg[x]-=1
    deg[y]+=1
if sz(a[0][0]) == len(V):
    # とりあえず連結
    start=0
    end=0
    for x in deg:
        if x==1:
            end+=1
        elif x==-1:
            start+=1
    if start==1 and end==1:print(1)
    elif start==0 and end==0:print(len(V))
    else: print(0)
else:
    print(0)
0