結果

問題 No.583 鉄道同好会
ユーザー tentententen
提出日時 2020-09-07 16:48:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 744 ms / 2,000 ms
コード長 1,830 bytes
コンパイル時間 5,435 ms
コンパイル使用メモリ 73,664 KB
実行使用メモリ 65,216 KB
最終ジャッジ日時 2023-08-19 16:50:26
合計ジャッジ時間 13,587 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,336 KB
testcase_01 AC 122 ms
55,896 KB
testcase_02 AC 126 ms
55,768 KB
testcase_03 AC 124 ms
55,632 KB
testcase_04 AC 123 ms
58,052 KB
testcase_05 AC 122 ms
55,596 KB
testcase_06 AC 124 ms
55,980 KB
testcase_07 AC 123 ms
55,744 KB
testcase_08 AC 126 ms
55,776 KB
testcase_09 AC 132 ms
55,844 KB
testcase_10 AC 164 ms
56,688 KB
testcase_11 AC 410 ms
60,804 KB
testcase_12 AC 493 ms
60,340 KB
testcase_13 AC 462 ms
57,960 KB
testcase_14 AC 462 ms
60,124 KB
testcase_15 AC 542 ms
60,488 KB
testcase_16 AC 670 ms
65,216 KB
testcase_17 AC 715 ms
64,684 KB
testcase_18 AC 744 ms
64,780 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[] counts = new int[n];
    	UnionFindTree uft = new UnionFindTree(n);
    	for (int i = 0; i < m; i++) {
    	    int a = sc.nextInt();
    	    int b = sc.nextInt();
    	    uft.unite(a, b);
    	    counts[a]++;
    	    counts[b]++;
    	}
   	    int count = 0;
    	for (int x : counts) {
    	    count += x % 2;
    	}
    	boolean flag = (count <= 2 && uft.isOne());
    	if (flag) {
    	    System.out.println("YES");
    	} else {
    	    System.out.println("NO");
    	}
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            counts = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                counts[i] = 1;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                counts[find(y)] += counts[find(x)];
                counts[find(x)] = 0;
                parents[find(x)] = find(y);
            }
        }
        
        public boolean isOne() {
            int count = 0;
            for (int i = 0; i < counts.length; i++) {
                if (counts[i] > 1) {
                    count++;
                }
            }
            return count <= 1;
        }
    }
}
0