結果
問題 | No.583 鉄道同好会 |
ユーザー | tenten |
提出日時 | 2022-10-14 17:24:10 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 244 ms / 2,000 ms |
コード長 | 2,357 bytes |
コンパイル時間 | 4,049 ms |
コンパイル使用メモリ | 75,080 KB |
実行使用メモリ | 58,372 KB |
最終ジャッジ日時 | 2023-09-08 19:46:00 |
合計ジャッジ時間 | 5,736 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
49,180 KB |
testcase_01 | AC | 44 ms
49,436 KB |
testcase_02 | AC | 44 ms
49,372 KB |
testcase_03 | AC | 43 ms
49,288 KB |
testcase_04 | AC | 43 ms
49,444 KB |
testcase_05 | AC | 43 ms
49,592 KB |
testcase_06 | AC | 44 ms
49,280 KB |
testcase_07 | AC | 45 ms
49,376 KB |
testcase_08 | AC | 44 ms
49,132 KB |
testcase_09 | AC | 45 ms
49,340 KB |
testcase_10 | AC | 50 ms
49,660 KB |
testcase_11 | AC | 149 ms
54,648 KB |
testcase_12 | AC | 168 ms
56,436 KB |
testcase_13 | AC | 165 ms
56,040 KB |
testcase_14 | AC | 167 ms
55,748 KB |
testcase_15 | AC | 178 ms
56,988 KB |
testcase_16 | AC | 231 ms
57,800 KB |
testcase_17 | AC | 242 ms
57,548 KB |
testcase_18 | AC | 244 ms
58,372 KB |
ソースコード
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(); } }