結果

問題 No.583 鉄道同好会
ユーザー kitakitalilykitakitalily
提出日時 2019-04-07 16:35:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,178 ms / 2,000 ms
コード長 1,570 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 74,040 KB
実行使用メモリ 70,496 KB
最終ジャッジ日時 2023-09-09 12:53:17
合計ジャッジ時間 12,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,868 KB
testcase_01 AC 121 ms
56,076 KB
testcase_02 AC 121 ms
55,612 KB
testcase_03 AC 120 ms
55,668 KB
testcase_04 AC 122 ms
55,536 KB
testcase_05 AC 133 ms
55,832 KB
testcase_06 AC 125 ms
57,808 KB
testcase_07 AC 116 ms
55,304 KB
testcase_08 AC 123 ms
55,864 KB
testcase_09 AC 128 ms
56,116 KB
testcase_10 AC 166 ms
56,232 KB
testcase_11 AC 520 ms
60,668 KB
testcase_12 AC 644 ms
63,932 KB
testcase_13 AC 608 ms
63,824 KB
testcase_14 AC 626 ms
63,520 KB
testcase_15 AC 738 ms
67,032 KB
testcase_16 AC 1,083 ms
68,904 KB
testcase_17 AC 1,178 ms
70,328 KB
testcase_18 AC 1,170 ms
70,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[] c = new int[n];
        ArrayList[] x = new ArrayList[n];
        for(int i=0; i<n; i++){
            x[i] = new ArrayList<Integer>();
        }
        
        int at = 0;
        for(int i=0; i<m; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            x[a].add(b);
            x[b].add(a);
            c[a]++;
            c[b]++;
            at = a;
        }
        
        int con = 0;
        ArrayList<Integer> q = new ArrayList<Integer>();
        q.add(at);
        boolean[] ch = new boolean[n];
        while(q.size()>0){
            int now = q.remove(q.size()-1);
            ch[now] = true;
            for(int i=0; i<x[now].size(); i++){
                int ne = (int)x[now].get(i);
                if(ch[ne]){
                    continue;
                }
                q.add(ne);
            }
            con++;
        }
        
        int uni = 0;
        int odd = 0;
        for(int i=0; i<n; i++){
            if(c[i]%2==1){
               odd++;
            }
            if(c[i]>0){
                uni++;
            }
        }
        
        if(con<uni){
            System.out.println("NO");
            System.exit(0);
        }
        
        if(odd==0 || odd==2){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }
    }
}
0