結果

問題 No.1605 Matrix Shape
ユーザー H3PO4H3PO4
提出日時 2021-07-16 21:50:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,161 ms / 2,000 ms
コード長 699 bytes
コンパイル時間 116 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 105,112 KB
最終ジャッジ日時 2023-09-20 13:38:01
合計ジャッジ時間 28,321 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 539 ms
46,996 KB
testcase_01 AC 228 ms
47,064 KB
testcase_02 AC 222 ms
47,012 KB
testcase_03 AC 238 ms
47,096 KB
testcase_04 AC 221 ms
47,024 KB
testcase_05 AC 231 ms
46,996 KB
testcase_06 AC 227 ms
47,044 KB
testcase_07 AC 219 ms
47,228 KB
testcase_08 AC 231 ms
46,916 KB
testcase_09 AC 229 ms
47,080 KB
testcase_10 AC 229 ms
47,124 KB
testcase_11 AC 231 ms
47,000 KB
testcase_12 AC 225 ms
46,840 KB
testcase_13 AC 230 ms
47,048 KB
testcase_14 AC 228 ms
47,076 KB
testcase_15 AC 233 ms
47,200 KB
testcase_16 AC 225 ms
47,060 KB
testcase_17 AC 228 ms
46,852 KB
testcase_18 AC 229 ms
46,992 KB
testcase_19 AC 1,161 ms
78,804 KB
testcase_20 AC 1,112 ms
75,388 KB
testcase_21 AC 1,113 ms
75,268 KB
testcase_22 AC 1,143 ms
78,812 KB
testcase_23 AC 1,152 ms
78,760 KB
testcase_24 AC 1,158 ms
79,104 KB
testcase_25 AC 971 ms
63,288 KB
testcase_26 AC 973 ms
62,688 KB
testcase_27 AC 976 ms
62,584 KB
testcase_28 AC 969 ms
62,696 KB
testcase_29 AC 974 ms
63,508 KB
testcase_30 AC 1,111 ms
75,328 KB
testcase_31 AC 1,112 ms
75,324 KB
testcase_32 AC 1,049 ms
72,220 KB
testcase_33 AC 1,034 ms
72,296 KB
testcase_34 AC 924 ms
66,176 KB
testcase_35 AC 935 ms
73,884 KB
testcase_36 AC 658 ms
105,112 KB
権限があれば一括ダウンロードができます

ソースコード

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