結果

問題 No.583 鉄道同好会
ユーザー htensaihtensai
提出日時 2019-12-12 19:09:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 771 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 3,259 ms
コンパイル使用メモリ 74,532 KB
実行使用メモリ 64,916 KB
最終ジャッジ日時 2023-09-07 19:01:19
合計ジャッジ時間 10,825 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,456 KB
testcase_01 AC 126 ms
55,604 KB
testcase_02 AC 123 ms
55,696 KB
testcase_03 AC 122 ms
55,756 KB
testcase_04 AC 122 ms
56,008 KB
testcase_05 AC 122 ms
55,864 KB
testcase_06 AC 122 ms
55,628 KB
testcase_07 AC 124 ms
55,956 KB
testcase_08 AC 129 ms
55,576 KB
testcase_09 AC 128 ms
56,032 KB
testcase_10 AC 172 ms
55,656 KB
testcase_11 AC 422 ms
60,280 KB
testcase_12 AC 500 ms
60,508 KB
testcase_13 AC 478 ms
60,460 KB
testcase_14 AC 491 ms
60,872 KB
testcase_15 AC 558 ms
60,428 KB
testcase_16 AC 710 ms
64,916 KB
testcase_17 AC 764 ms
64,652 KB
testcase_18 AC 771 ms
64,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] counts = new int[n];
        UnionFindTree uft = new UnionFindTree(n);
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            counts[x]++;
            counts[y]++;
            uft.unite(x, y);
            set.add(x);
            set.add(y);
        }
        boolean flag = true;
        int parent = -1;
        for (int x : set) {
            int y = uft.find(x);
            if (parent != -1 && parent != y) {
                flag = false;
                break;
            }
            parent = y;
        }
        int od = 0;
        for (int i = 0; i < n; i++) {
            if (counts[i] % 2 == 1) {
                od++;
            }
        }
        if (od > 2 || !flag) {
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int  find(int x) {
            if(parents[x] == x) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            parents[xx] = yy;
        }
    }
}
0