結果
| 問題 |
No.583 鉄道同好会
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-10-14 17:24:10 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 282 ms / 2,000 ms |
| コード長 | 2,357 bytes |
| コンパイル時間 | 2,835 ms |
| コンパイル使用メモリ | 78,184 KB |
| 実行使用メモリ | 46,660 KB |
| 最終ジャッジ日時 | 2024-06-26 12:41:34 |
| 合計ジャッジ時間 | 5,855 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
int[] counts = new int[n];
UnionFindTree uft = new UnionFindTree(n);
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
counts[x]++;
counts[y]++;
uft.unite(x, y);
}
int judge = 0;
HashSet<Integer> parents = new HashSet<>();
for (int i = 0; i < n; i++) {
judge += counts[i] % 2;
if (counts[i] > 0) {
parents.add(uft.find(i));
}
}
if (judge <= 2 && parents.size() == 1) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
static class UnionFindTree {
int[] parents;
public UnionFindTree(int size) {
parents = new int[size];
for (int i = 0; i < size; i++) {
parents[i] = i;
}
}
public int find(int x) {
if (x == parents[x]) {
return x;
} else {
return parents[x] = find(parents[x]);
}
}
public boolean same(int x, int y) {
return find(x) == find(y);
}
public void unite(int x, int y) {
if (!same(x, y)) {
parents[find(x)] = find(y);
}
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten