結果

問題 No.583 鉄道同好会
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-04-12 21:09:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,421 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 71,788 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-12 17:20:07
合計ジャッジ時間 3,208 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 1 ms
4,368 KB
testcase_11 AC 3 ms
4,372 KB
testcase_12 AC 3 ms
4,372 KB
testcase_13 AC 3 ms
4,368 KB
testcase_14 AC 3 ms
4,372 KB
testcase_15 AC 3 ms
4,372 KB
testcase_16 AC 4 ms
4,372 KB
testcase_17 AC 4 ms
4,372 KB
testcase_18 AC 4 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

class UnionFind {
  vector<int> par;
 public:
  explicit UnionFind(int n) : par(n, -1) {}
  int root(int a) {
    if(par[a] < 0) { return a; }
    return par[a] = root(par[a]);
  }
  int size(int a) {
    return -par[root(a)];
  }
  void unite(int a, int b) {
    a = root(a);
    b = root(b);
    if(a == b) { return; }
    if(size(a) < size(b)) { swap(a, b); }
    par[a] += par[b];
    par[b] = a;
  }
};

template <class T>
inline void readint(T *x) {
  *x = 0;
  int c;
  for(;;) {
    c = getchar_unlocked();
    switch(c) case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: goto num;
  }

num:
  for(;;) {
    switch(c) {
      case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
        *x *= 10;
        *x += c - 48;
        break;
      default:
        return;
    }
    c = getchar_unlocked();
  }
}

int main(void) {
  int N, M; readint(&N); readint(&M);
  UnionFind uf(N);
  vector<int> deg(N);
  int x = 0;
  for(int i=0; i<M; ++i) {
    int u, v; readint(&u); readint(&v);
    uf.unite(u, v);
    ++deg[u];
    ++deg[v];
    x = v;
  }
  int k = 0;
  for(int v=0; v<N; ++v) {
    if(!deg[v]) { continue; }
    if(deg[v] & 1) { ++k; }
    if(uf.root(x) != uf.root(v)) {
      puts("NO");
      return 0;
    }
  }
  puts(k == 0 || k == 2 ? "YES" : "NO");
  return 0;
}
0