結果

問題 No.583 鉄道同好会
ユーザー uafr_csuafr_cs
提出日時 2017-11-08 20:58:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,230 ms / 2,000 ms
コード長 1,443 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 79,168 KB
実行使用メモリ 75,128 KB
最終ジャッジ日時 2024-05-03 07:52:57
合計ジャッジ時間 12,218 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
41,376 KB
testcase_01 AC 132 ms
41,620 KB
testcase_02 AC 135 ms
41,216 KB
testcase_03 AC 139 ms
41,292 KB
testcase_04 AC 137 ms
41,392 KB
testcase_05 AC 136 ms
41,560 KB
testcase_06 AC 135 ms
41,228 KB
testcase_07 AC 136 ms
41,372 KB
testcase_08 AC 136 ms
41,140 KB
testcase_09 AC 147 ms
41,424 KB
testcase_10 AC 191 ms
41,976 KB
testcase_11 AC 611 ms
51,684 KB
testcase_12 AC 700 ms
52,508 KB
testcase_13 AC 659 ms
52,392 KB
testcase_14 AC 698 ms
52,420 KB
testcase_15 AC 804 ms
54,476 KB
testcase_16 AC 1,105 ms
71,788 KB
testcase_17 AC 1,230 ms
75,128 KB
testcase_18 AC 1,219 ms
74,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int M = sc.nextInt();
		
		ArrayList<HashSet<Integer>> adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){ adj.add(new HashSet<Integer>()); }
		
		int[] degs = new int[N];
		for(int i = 0; i < M; i++){
			final int a = sc.nextInt();
			final int b = sc.nextInt();
			
			adj.get(a).add(b);
			adj.get(b).add(a);
			
			degs[a]++; degs[b]++;
		}
		
		boolean[] visited = new boolean[N];
		LinkedList<Integer> queue = new LinkedList<Integer>();
		for(int i = 0; i < N; i++){ 
			if(degs[i] > 0){
				visited[i] = true;
				queue.add(i);
				break;
			}
		}
		
		while(!queue.isEmpty()){
			final int node = queue.poll();
			
			for(final int next : adj.get(node)){
				if(visited[next]){ continue; }
				
				visited[next] = true;
				queue.add(next);
			}
		}
		//System.out.println(Arrays.toString(visited));
		
		for(int i = 0; i < N; i++){
			if(degs[i] > 0 && !visited[i]){
				System.out.println("NO");
				return;
			}
		}
		
		int odd_count = 0;
		for(int i = 0; i < N; i++){
			if(degs[i] % 2 == 1){ odd_count++; }
		}
		
		if(odd_count == 0 || odd_count == 2){
			System.out.println("YES");
		}else{
			System.out.println("NO");
		}
	}
}
0