結果
| 問題 | No.1605 Matrix Shape |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-16 21:50:38 |
| 言語 | Python3 (3.14.3 + numpy 2.4.2 + scipy 1.17.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 699 bytes |
| 記録 | |
| コンパイル時間 | 510 ms |
| コンパイル使用メモリ | 20,828 KB |
| 実行使用メモリ | 101,028 KB |
| 最終ジャッジ日時 | 2026-03-27 20:36:03 |
| 合計ジャッジ時間 | 51,548 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 TLE * 22 |
ソースコード
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)