結果

問題 No.1565 Union
ユーザー lloyzlloyz
提出日時 2021-12-18 00:12:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,117 ms / 2,000 ms
コード長 537 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 66,592 KB
最終ジャッジ日時 2023-12-24 03:25:22
合計ジャッジ時間 18,865 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,112 KB
testcase_01 AC 29 ms
10,112 KB
testcase_02 AC 29 ms
10,112 KB
testcase_03 AC 27 ms
10,112 KB
testcase_04 AC 26 ms
10,112 KB
testcase_05 AC 26 ms
10,112 KB
testcase_06 AC 27 ms
10,112 KB
testcase_07 AC 27 ms
10,112 KB
testcase_08 AC 27 ms
10,112 KB
testcase_09 AC 27 ms
10,112 KB
testcase_10 AC 452 ms
20,736 KB
testcase_11 AC 595 ms
37,372 KB
testcase_12 AC 749 ms
29,440 KB
testcase_13 AC 227 ms
18,556 KB
testcase_14 AC 828 ms
40,156 KB
testcase_15 AC 928 ms
48,044 KB
testcase_16 AC 843 ms
44,324 KB
testcase_17 AC 1,041 ms
52,984 KB
testcase_18 AC 1,072 ms
52,880 KB
testcase_19 AC 1,117 ms
52,872 KB
testcase_20 AC 910 ms
66,592 KB
testcase_21 AC 904 ms
66,592 KB
testcase_22 AC 910 ms
66,592 KB
testcase_23 AC 916 ms
66,592 KB
testcase_24 AC 865 ms
58,308 KB
testcase_25 AC 938 ms
66,592 KB
testcase_26 AC 795 ms
54,264 KB
testcase_27 AC 878 ms
58,308 KB
testcase_28 AC 937 ms
66,592 KB
testcase_29 AC 927 ms
66,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict

n, m = map(int, input().split())
edge = defaultdict(list)
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edge[a].append(b)
    edge[b].append(a)

ans = -1
visited = set([0])
Que = deque([(0, 0)])
while Que:
    curr, cnt = Que.popleft()
    if curr == n - 1:
        ans = cnt
        break
    for np in edge[curr]:
        if np in visited:
            continue
        visited.add(np)
        Que.append((np, cnt + 1))
print(ans)
0