結果
| 問題 |
No.1565 Union
|
| コンテスト | |
| ユーザー |
bluemegane
|
| 提出日時 | 2021-06-26 18:46:48 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 462 ms / 2,000 ms |
| コード長 | 1,320 bytes |
| コンパイル時間 | 5,642 ms |
| コンパイル使用メモリ | 114,356 KB |
| 実行使用メモリ | 43,760 KB |
| 最終ジャッジ日時 | 2024-12-20 20:20:32 |
| 合計ジャッジ時間 | 13,111 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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];
q.Enqueue(new P { to = 0, step = 0 });
visited[0] = 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);
}
}
bluemegane