結果

問題 No.583 鉄道同好会
ユーザー takeya_okinotakeya_okino
提出日時 2017-10-28 00:17:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 751 ms / 2,000 ms
コード長 1,331 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 73,692 KB
実行使用メモリ 49,912 KB
最終ジャッジ日時 2023-08-14 05:31:45
合計ジャッジ時間 9,122 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
39,732 KB
testcase_01 AC 119 ms
39,312 KB
testcase_02 AC 122 ms
39,268 KB
testcase_03 AC 120 ms
39,168 KB
testcase_04 AC 129 ms
39,900 KB
testcase_05 AC 119 ms
39,388 KB
testcase_06 AC 120 ms
39,752 KB
testcase_07 AC 120 ms
39,464 KB
testcase_08 AC 123 ms
39,464 KB
testcase_09 AC 125 ms
39,744 KB
testcase_10 AC 173 ms
40,428 KB
testcase_11 AC 422 ms
45,580 KB
testcase_12 AC 496 ms
45,456 KB
testcase_13 AC 477 ms
45,756 KB
testcase_14 AC 484 ms
45,692 KB
testcase_15 AC 545 ms
45,584 KB
testcase_16 AC 682 ms
49,912 KB
testcase_17 AC 751 ms
49,012 KB
testcase_18 AC 746 ms
48,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int[] par;
  static int[] rank;

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    par = new int[n];
    rank = new int[n];
    for(int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
    int kiten = 0;
    int[] e = new int[n];
    for(int i = 0; i < m; i++) {
      int a = sc.nextInt();
      int b = sc.nextInt();
      e[a]++;
      e[b]++;
      unite(a, b);
    }
    for(int i = 0; i < n; i++) {
      if(e[i] % 2 == 1) kiten++;
    }
    int count = 0;
    for(int i = 0; i < n; i++) {
      for(int j = i + 1; j < n; j++) {
        if((e[i] > 0) && (e[j] > 0)) {
          if(!same(i, j)) count++;
        }
      }
    }
    String ans = "NO";
    if((count == 0) && ((kiten == 0) || (kiten == 2))) ans = "YES";
    System.out.println(ans);
  }

  public static int find(int x) {
    if(par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  public static void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if(rank[x] == rank[y]) rank[x]++;
    }
  }

  public static boolean same(int x, int y) {
    return find(x) == find(y);
  }
}
0