結果
| 問題 |
No.583 鉄道同好会
|
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2017-10-27 23:07:53 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 985 ms / 2,000 ms |
| コード長 | 1,374 bytes |
| コンパイル時間 | 3,000 ms |
| コンパイル使用メモリ | 77,932 KB |
| 実行使用メモリ | 68,592 KB |
| 最終ジャッジ日時 | 2024-11-21 23:02:18 |
| 合計ジャッジ時間 | 11,007 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
import java.util.*;
class Main {
ArrayList<Integer>[] graph;
@SuppressWarnings("unchecked")
Main() {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
this.graph = new ArrayList[n];
for (int i = 0; i < n; ++i) {
this.graph[i] = new ArrayList();
}
int m = scan.nextInt();
for (int i = 0; i < m; ++i) {
int sa = scan.nextInt();
int sb = scan.nextInt();
this.graph[sa].add(sb);
this.graph[sb].add(sa);
}
int numberOfOddVertices = 0;
for (int i = 0; i < n; ++i) {
if (graph[i].size() % 2 != 0) {
numberOfOddVertices += 1;
}
}
if (numberOfOddVertices > 2) {
System.out.println("NO");
return;
}
// Henga renketu ka douka siraberu
int start = 0;
for (int i = 0; i < n; ++i) {
if (graph[i].size() >= 1) {
start = i;
break;
}
}
boolean[] visited = new boolean[n];
Queue<Integer> queue = new ArrayDeque();
queue.add(start);
while (queue.size() >= 1) {
int vertex = queue.poll();
if (visited[vertex]) {
continue;
}
visited[vertex] = true;
for (int target: this.graph[vertex]) {
queue.add(target);
}
}
for (int i = 0; i < n; ++i) {
if (graph[i].size() >= 1 && !visited[i]) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
public static void main(String[] args) {
new Main();
}
}
夕叢霧香(ゆうむらきりか)