結果

問題 No.583 鉄道同好会
ユーザー CleyLCleyL
提出日時 2023-03-26 00:46:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 89,180 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 08:42:37
合計ジャッジ時間 2,258 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 17 ms
4,348 KB
testcase_12 AC 24 ms
4,348 KB
testcase_13 AC 24 ms
4,348 KB
testcase_14 AC 24 ms
4,348 KB
testcase_15 AC 31 ms
4,348 KB
testcase_16 AC 56 ms
4,348 KB
testcase_17 AC 67 ms
4,348 KB
testcase_18 AC 67 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:63:29: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
   63 |   if((odd == 0 || odd == 2) && d == x)cout << "YES" << endl;
      |      ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
main.cpp:58:14: note: 'x' was declared here
   58 |   int d = n, x;
      |              ^

ソースコード

diff #

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

struct UnionFind {
  int n;
  vector<int> par;
  vector<int> size_;
  UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
    for(int i = 0; n > i; i++)par[i] = i;
  }

  int root(int x){
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    size_[rb] += size_[ra];
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};

int main(){
  int n,m;cin>>n>>m;
  map<int,int> A;
  UnionFind B(n);
  for(int i = 0; m > i; i++){
    int a,b;cin>>a>>b;
    A[a]++;A[b]++;
    B.unite(a,b);
  }
  int odd = 0;
  for(auto x: A){
    odd += x.second%2;
  }
  int d = n, x;
  for(int i = 0; n > i; i++){
    if(B.size(i) == 1)d--;
    else x = B.size(i);
  }
  if((odd == 0 || odd == 2) && d == x)cout << "YES" << endl;
  else cout << "NO" << endl;
}
0