結果

問題 No.583 鉄道同好会
ユーザー neko_the_shadow
提出日時 2019-05-17 18:04:39
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,351 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 89,972 KB
実行使用メモリ 65,628 KB
最終ジャッジ日時 2024-09-17 05:58:39
合計ジャッジ時間 9,613 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

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