結果

問題 No.583 鉄道同好会
ユーザー sca1lsca1l
提出日時 2017-10-28 00:24:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,492 ms / 2,000 ms
コード長 1,570 bytes
コンパイル時間 2,670 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 65,392 KB
最終ジャッジ日時 2024-05-01 18:21:52
合計ジャッジ時間 12,561 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
40,140 KB
testcase_01 AC 136 ms
41,464 KB
testcase_02 AC 118 ms
40,260 KB
testcase_03 AC 114 ms
39,876 KB
testcase_04 AC 124 ms
40,392 KB
testcase_05 AC 130 ms
41,288 KB
testcase_06 AC 129 ms
41,292 KB
testcase_07 AC 129 ms
41,496 KB
testcase_08 AC 134 ms
41,504 KB
testcase_09 AC 122 ms
40,416 KB
testcase_10 AC 186 ms
42,564 KB
testcase_11 AC 597 ms
49,504 KB
testcase_12 AC 637 ms
51,012 KB
testcase_13 AC 730 ms
50,956 KB
testcase_14 AC 703 ms
53,168 KB
testcase_15 AC 792 ms
54,732 KB
testcase_16 AC 1,371 ms
64,700 KB
testcase_17 AC 1,492 ms
65,392 KB
testcase_18 AC 1,466 ms
64,276 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