Submission #39464374


Source Code Expand

#include<bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

struct UnionFind {
  vector<int> parent;
  vector<int> size;
  vector<int> edgeCnt;

  UnionFind(int n){
    parent.resize(n);
    size.resize(n);
    edgeCnt.resize(n);
    for(int i = 0; i < n; ++i){
      parent[i] = i;
      size[i] = 1;
      edgeCnt[i] = 0;
    }
  }

  int leader(int x) {
    if(parent[x] == x) return x;
    return parent[x] = leader(parent[x]);
  }

  bool merge(int x, int y) {
    x = leader(x);
    y = leader(y);
    if(x == y){
      edgeCnt[x]++;
      return false;
    }
    if(size[x] < size[y]) swap(x, y);
    parent[y] = x;
    size[x] += size[y];
    edgeCnt[x] += edgeCnt[y] + 1;
    return true;
  }

  bool equalsNumVerticesAndNumEdges(int x){
    return size[leader(x)] == edgeCnt[leader(x)];
  }
};

int main(){
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for(int i = 0; i < M; ++i){
    int u, v;
    cin >> u >> v;
    uf.merge(u-1, v-1);
  }

  // それぞれの連結成分で頂点数と辺数が一致しないものがあればNo
  for(int i = 0; i < N; ++i){
    if(!uf.equalsNumVerticesAndNumEdges(i)){
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

Submission Info

Submission Time
Task D - Unicyclic Components
User dokaraya
Language C++ (GCC 9.2.1)
Score 400
Code Size 1301 Byte
Status AC
Exec Time 105 ms
Memory 5636 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 61
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_rnd_00.txt, 01_rnd_01.txt, 01_rnd_02.txt, 01_rnd_03.txt, 01_rnd_04.txt, 01_rnd_05.txt, 01_rnd_06.txt, 01_rnd_07.txt, 01_rnd_08.txt, 02_one_00.txt, 02_one_01.txt, 02_one_02.txt, 02_one_03.txt, 02_one_04.txt, 02_one_05.txt, 02_one_06.txt, 02_one_07.txt, 02_one_08.txt, 02_one_09.txt, 02_one_10.txt, 02_one_11.txt, 02_one_12.txt, 02_one_13.txt, 03_two_00.txt, 03_two_01.txt, 03_two_02.txt, 03_two_03.txt, 03_two_04.txt, 03_two_05.txt, 03_two_06.txt, 03_two_07.txt, 03_two_08.txt, 03_two_09.txt, 03_two_10.txt, 03_two_11.txt, 04_many_00.txt, 04_many_01.txt, 04_many_02.txt, 04_many_03.txt, 04_many_04.txt, 04_many_05.txt, 04_many_06.txt, 04_many_07.txt, 04_many_08.txt, 04_many_09.txt, 04_many_10.txt, 04_many_11.txt, 05_hand_00.txt, 05_hand_01.txt, 99_hack_00.txt, 99_hack_01.txt, 99_hack_02.txt, 99_hack_03.txt, 99_hack_04.txt, 99_hack_05.txt, 99_hack_06.txt, 99_hack_07.txt, 99_hack_08.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 5 ms 3544 KiB
00_sample_01.txt AC 2 ms 3392 KiB
00_sample_02.txt AC 2 ms 3404 KiB
01_rnd_00.txt AC 3 ms 3596 KiB
01_rnd_01.txt AC 2 ms 3396 KiB
01_rnd_02.txt AC 51 ms 3556 KiB
01_rnd_03.txt AC 3 ms 3508 KiB
01_rnd_04.txt AC 2 ms 3476 KiB
01_rnd_05.txt AC 63 ms 3604 KiB
01_rnd_06.txt AC 9 ms 5552 KiB
01_rnd_07.txt AC 7 ms 5436 KiB
01_rnd_08.txt AC 105 ms 5492 KiB
02_one_00.txt AC 2 ms 3392 KiB
02_one_01.txt AC 3 ms 3612 KiB
02_one_02.txt AC 4 ms 3540 KiB
02_one_03.txt AC 2 ms 3596 KiB
02_one_04.txt AC 3 ms 3592 KiB
02_one_05.txt AC 2 ms 3592 KiB
02_one_06.txt AC 2 ms 3540 KiB
02_one_07.txt AC 90 ms 5408 KiB
02_one_08.txt AC 90 ms 5572 KiB
02_one_09.txt AC 90 ms 5576 KiB
02_one_10.txt AC 91 ms 5568 KiB
02_one_11.txt AC 91 ms 5460 KiB
02_one_12.txt AC 92 ms 5564 KiB
02_one_13.txt AC 92 ms 5440 KiB
03_two_00.txt AC 90 ms 5460 KiB
03_two_01.txt AC 90 ms 5488 KiB
03_two_02.txt AC 92 ms 5464 KiB
03_two_03.txt AC 90 ms 5440 KiB
03_two_04.txt AC 92 ms 5492 KiB
03_two_05.txt AC 92 ms 5436 KiB
03_two_06.txt AC 89 ms 5636 KiB
03_two_07.txt AC 91 ms 5632 KiB
03_two_08.txt AC 90 ms 5512 KiB
03_two_09.txt AC 89 ms 5488 KiB
03_two_10.txt AC 89 ms 5408 KiB
03_two_11.txt AC 94 ms 5436 KiB
04_many_00.txt AC 66 ms 5052 KiB
04_many_01.txt AC 63 ms 5004 KiB
04_many_02.txt AC 66 ms 5048 KiB
04_many_03.txt AC 65 ms 4876 KiB
04_many_04.txt AC 89 ms 5436 KiB
04_many_05.txt AC 91 ms 5552 KiB
04_many_06.txt AC 92 ms 5560 KiB
04_many_07.txt AC 91 ms 5424 KiB
04_many_08.txt AC 85 ms 5372 KiB
04_many_09.txt AC 84 ms 5292 KiB
04_many_10.txt AC 84 ms 5268 KiB
04_many_11.txt AC 84 ms 5288 KiB
05_hand_00.txt AC 2 ms 3596 KiB
05_hand_01.txt AC 93 ms 5552 KiB
99_hack_00.txt AC 2 ms 3612 KiB
99_hack_01.txt AC 2 ms 3408 KiB
99_hack_02.txt AC 3 ms 3468 KiB
99_hack_03.txt AC 4 ms 3468 KiB
99_hack_04.txt AC 2 ms 3468 KiB
99_hack_05.txt AC 3 ms 3612 KiB
99_hack_06.txt AC 2 ms 3452 KiB
99_hack_07.txt AC 2 ms 3592 KiB
99_hack_08.txt AC 2 ms 3588 KiB