結果

問題 No.1605 Matrix Shape
ユーザー titia
提出日時 2021-07-17 01:57:49
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 922 ms / 2,000 ms
コード長 1,483 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 85,584 KB
最終ジャッジ日時 2024-07-06 13:13:02
合計ジャッジ時間 14,836 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

# なぜか、「終点と終点の前の頂点」の組としてありうる個数を数えようとしていたのはおかしい。
# 行列の積なんだから始点と終点です。はい。
# その後の考察もおかしかった。オイラー路に対する理解もおかしかった。

import sys
input = sys.stdin.readline

N=int(input())
HW=[tuple(map(int,input().split())) for i in range(N)]

DEG=[0]*(2*10**5+1)

# UnionFind

Group = [i for i in range(2*10**5+1)] # グループ分け
Nodes = [1]*(2*10**5+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for h,w in HW:
    Union(h,w)
    DEG[h]+=1
    DEG[w]-=1

X=find(HW[0][0])

for h,w in HW:
    if find(h)!=X or find(w)!=X:
        print(0)
        exit()

one=0
minus=0
other=0

for i in range(2*10**5+1):
    if DEG[i]==1:
        one+=1
    elif DEG[i]==-1:
        minus+=1
    elif DEG[i]!=0:
        other+=1

if other!=0:
    print(0)
elif one==0 and minus==0:
    print(len(set([h for h,w in HW])|set([w for h,w in HW])))
elif one==1 and minus==1:
    print(1)
else:
    print(0)




0