結果
| 問題 | No.583 鉄道同好会 | 
| コンテスト | |
| ユーザー |  takeya_okino | 
| 提出日時 | 2017-10-28 00:17:24 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 765 ms / 2,000 ms | 
| コード長 | 1,331 bytes | 
| コンパイル時間 | 2,225 ms | 
| コンパイル使用メモリ | 77,008 KB | 
| 実行使用メモリ | 53,736 KB | 
| 最終ジャッジ日時 | 2024-11-21 23:57:12 | 
| 合計ジャッジ時間 | 9,142 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 16 | 
ソースコード
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);
  }
}
            
            
            
        