結果

問題 No.583 鉄道同好会
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 18:23:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 891 ms / 2,000 ms
コード長 2,521 bytes
コンパイル時間 2,631 ms
コンパイル使用メモリ 91,032 KB
実行使用メモリ 72,496 KB
最終ジャッジ日時 2023-10-17 07:23:56
合計ジャッジ時間 10,851 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
57,392 KB
testcase_01 AC 140 ms
57,420 KB
testcase_02 AC 143 ms
57,388 KB
testcase_03 AC 144 ms
57,708 KB
testcase_04 AC 143 ms
57,568 KB
testcase_05 AC 140 ms
57,436 KB
testcase_06 AC 143 ms
57,432 KB
testcase_07 AC 144 ms
57,544 KB
testcase_08 AC 148 ms
57,740 KB
testcase_09 AC 152 ms
57,412 KB
testcase_10 AC 186 ms
57,548 KB
testcase_11 AC 524 ms
62,064 KB
testcase_12 AC 584 ms
62,420 KB
testcase_13 AC 572 ms
62,352 KB
testcase_14 AC 555 ms
62,568 KB
testcase_15 AC 639 ms
63,952 KB
testcase_16 AC 824 ms
68,964 KB
testcase_17 AC 887 ms
72,448 KB
testcase_18 AC 891 ms
72,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        String ans = (isOK()) ? "YES" : "NO";
        System.out.println(ans);
    }
    
    public static boolean isOK() {
        Scanner stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        int m = stdin.nextInt();
        
        UnionFind uf = new UnionFind(n);
        Map<Integer, Integer> counter = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int a = stdin.nextInt();
            int b = stdin.nextInt();
            uf.union(a, b);
            counter.put(a, counter.getOrDefault(a, 0) + 1);
            counter.put(b, counter.getOrDefault(b, 0) + 1);
        }
        
        List<Integer> keys = counter.keySet().stream().collect(Collectors.toList());
        for (int i = 0; i < keys.size(); i++) {
            for (int j = 0; j < keys.size(); j++) {
                if (uf.find(keys.get(i)) != uf.find(keys.get(j))) {
                    return false;
                }
            }
        }
       
        // Wikipedia「一筆書き」より抜粋:
        // ある連結グラフが一筆書き可能な場合の必要十分条件は、以下の条件のいずれか一方が成り立つことである(オイラー路参照)。 
        // すべての頂点の次数(頂点につながっている辺の数)が偶数 →運筆が起点に戻る場合(閉路)
        // 次数が奇数である頂点の数が2で、残りの頂点の次数は全て偶数 →運筆が起点に戻らない場合(閉路でない路)
        
        // <<< 奇数の頂点数が0または2 >>>
        long odd = counter.values().stream().filter(v -> v % 2 != 0).count();
        return odd == 0 || odd == 2;
    }
    
    private static class UnionFind {
        private int[] parents;
        
        public UnionFind(int n) {
            parents = IntStream.range(0, n).toArray();
        }
        
        public int find(int x) {
            if (parents[x] == x) {
                return x;
            } else {
                parents[x] = find(parents[x]);
                return parents[x];
            }
        }
        
        public void union(int x, int y) {
            x = find(x);
            y = find(y);
            if (x != y) {
                parents[y] = x; 
            }
        }
    }
}
0