結果

問題 No.1605 Matrix Shape
ユーザー H3PO4
提出日時 2021-07-16 21:50:38
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 699 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 124,700 KB
最終ジャッジ日時 2024-07-06 08:54:25
合計ジャッジ時間 61,724 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 TLE * 1
other AC * 19 TLE * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

from collections import Counter

N = int(input())
MAX = 2 * 10 ** 5
deg = [0] * MAX
st = set()
frm, to = [], []
for _ in range(N):
    H, W = (int(x) - 1 for x in input().split())
    deg[H] += 1
    deg[W] -= 1
    frm.append(H)
    to.append(W)
    st.add(H)
    st.add(W)

matr = csr_matrix(([1] * N, (frm, to)), shape=(MAX, MAX))
_, labels = connected_components(matr)

# 連結か
if len(set(labels[x] for x in st)) == 1:
    ct = Counter(deg)
    if ct[0] == MAX:
        print(len(st))
    elif ct[0] == MAX - 2 and ct[-1] == ct[1] == 1:
        print(1)
    else:
        print(0)
else:
    print(0)
0