結果

問題 No.583 鉄道同好会
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 18:04:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,351 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 89,204 KB
実行使用メモリ 72,464 KB
最終ジャッジ日時 2023-10-17 07:23:43
合計ジャッジ時間 10,463 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
57,156 KB
testcase_01 AC 127 ms
57,404 KB
testcase_02 AC 129 ms
55,716 KB
testcase_03 AC 127 ms
57,412 KB
testcase_04 WA -
testcase_05 AC 128 ms
57,784 KB
testcase_06 AC 128 ms
57,552 KB
testcase_07 AC 130 ms
57,516 KB
testcase_08 AC 121 ms
56,304 KB
testcase_09 AC 123 ms
56,396 KB
testcase_10 AC 178 ms
57,868 KB
testcase_11 AC 437 ms
61,888 KB
testcase_12 AC 489 ms
62,080 KB
testcase_13 AC 479 ms
61,132 KB
testcase_14 AC 482 ms
62,000 KB
testcase_15 AC 563 ms
61,908 KB
testcase_16 AC 716 ms
67,892 KB
testcase_17 AC 746 ms
72,464 KB
testcase_18 AC 770 ms
71,188 KB
権限があれば一括ダウンロードができます

ソースコード

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