結果
問題 |
No.1565 Union
|
ユーザー |
![]() |
提出日時 | 2021-06-26 18:41:52 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 458 ms / 2,000 ms |
コード長 | 1,408 bytes |
コンパイル時間 | 941 ms |
コンパイル使用メモリ | 114,328 KB |
実行使用メモリ | 52,024 KB |
最終ジャッジ日時 | 2024-09-27 07:57:23 |
合計ジャッジ時間 | 9,519 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System.Linq; using System.Collections.Generic; using System; public class P { public int to { get; set; } public int step { get; set; } } public class Hello { public static int n, m; static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); n = int.Parse(line[0]); m = int.Parse(line[1]); var aa = new List<int>[n]; for (int i = 0; i < n; i++) aa[i] = new List<int>(); for (int i = 0; i < m; i++) { line = Console.ReadLine().Trim().Split(' '); var a = int.Parse(line[0]) - 1; var b = int.Parse(line[1]) - 1; aa[a].Add(b); aa[b].Add(a); } getAns(aa); } static void getAns(List<int>[] aa) { var q = new Queue<P>(); var visited = new bool[n]; visited[0] = true; foreach (var x in aa[0]) { q.Enqueue(new P { to = x, step = 1 }); visited[x] = true; } while (q.Count() > 0) { var t = q.Dequeue(); if (t.to == n - 1) { Console.WriteLine(t.step); return; } foreach (var x in aa[t.to]) { if (visited[x]) continue; q.Enqueue(new P { to = x, step = t.step + 1 }); visited[x] = true; } } Console.WriteLine(-1); } }