結果

問題 No.1565 Union
ユーザー tobusakanatobusakana
提出日時 2022-12-17 19:41:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 569 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 102,412 KB
最終ジャッジ日時 2024-04-28 00:55:44
合計ジャッジ時間 7,439 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,984 KB
testcase_01 AC 38 ms
54,428 KB
testcase_02 AC 41 ms
54,456 KB
testcase_03 AC 42 ms
53,948 KB
testcase_04 AC 39 ms
54,924 KB
testcase_05 AC 39 ms
54,992 KB
testcase_06 AC 39 ms
54,400 KB
testcase_07 AC 37 ms
54,744 KB
testcase_08 AC 37 ms
55,416 KB
testcase_09 AC 39 ms
54,624 KB
testcase_10 AC 153 ms
88,112 KB
testcase_11 AC 228 ms
94,116 KB
testcase_12 AC 217 ms
92,448 KB
testcase_13 AC 122 ms
81,280 KB
testcase_14 AC 266 ms
94,464 KB
testcase_15 AC 299 ms
98,880 KB
testcase_16 AC 264 ms
98,304 KB
testcase_17 AC 330 ms
102,400 KB
testcase_18 AC 351 ms
102,152 KB
testcase_19 AC 384 ms
102,412 KB
testcase_20 AC 201 ms
100,388 KB
testcase_21 AC 207 ms
100,528 KB
testcase_22 AC 207 ms
100,796 KB
testcase_23 AC 204 ms
100,744 KB
testcase_24 AC 203 ms
100,564 KB
testcase_25 AC 224 ms
100,796 KB
testcase_26 AC 203 ms
99,908 KB
testcase_27 AC 214 ms
100,336 KB
testcase_28 AC 232 ms
100,400 KB
testcase_29 AC 217 ms
100,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
G = [[] for i in range(N)]
for _ in range(M):
    a,b = map(int,input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
    
from collections import deque
q = deque([])
q.append([0, -1, 0])
visited = [False] * N
while q:
    v,p,c = q.popleft()
    if visited[v]:
        continue
    visited[v] = True
    if v == N - 1:
        print(c)
        break
    for child in G[v]:
        if child == p:
            continue
        if visited[child]:
            continue
        q.append([child, v, c + 1])
else:
    print(-1)
0