結果
問題 |
No.583 鉄道同好会
|
ユーザー |
![]() |
提出日時 | 2019-12-12 19:09:57 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 910 ms / 2,000 ms |
コード長 | 1,823 bytes |
コンパイル時間 | 2,350 ms |
コンパイル使用メモリ | 78,312 KB |
実行使用メモリ | 65,564 KB |
最終ジャッジ日時 | 2024-06-25 12:47:50 |
合計ジャッジ時間 | 10,942 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] counts = new int[n]; UnionFindTree uft = new UnionFindTree(n); HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) { int x = sc.nextInt(); int y = sc.nextInt(); counts[x]++; counts[y]++; uft.unite(x, y); set.add(x); set.add(y); } boolean flag = true; int parent = -1; for (int x : set) { int y = uft.find(x); if (parent != -1 && parent != y) { flag = false; break; } parent = y; } int od = 0; for (int i = 0; i < n; i++) { if (counts[i] % 2 == 1) { od++; } } if (od > 2 || !flag) { System.out.println("NO"); } else { System.out.println("YES"); } } 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(parents[x] == x) { return x; } else { return parents[x] = find(parents[x]); } } public void unite(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } parents[xx] = yy; } } }