Submission #423134


Source Code Expand

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main() {
  int N, Q;
  int P, A, B;

  UnionFind uf(N);

  cin >> N >> Q;
  for (int i = 0; i < Q; ++i) {
    cin >> P >> A >> B;
    if (P == 0) {
      uf.unionSet(A, B);
    }
    if (P == 1) {
      if (uf.findSet(A, B)) {
        cout << "Yes" << endl;
      }else {
        cout << "No" << endl;
      }

    }
  }
  return 0;
}

Submission Info

Submission Time
Task B - Union Find
User otomarukanta
Language C++ (GCC 4.9.2)
Score 0
Code Size 979 Byte
Status RE
Exec Time 282 ms
Memory 928 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
RE × 1
RE × 19
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt RE 273 ms 800 KB
subtask_01_01.txt RE 274 ms 920 KB
subtask_01_02.txt RE 275 ms 796 KB
subtask_01_03.txt RE 272 ms 916 KB
subtask_01_04.txt RE 278 ms 800 KB
subtask_01_05.txt RE 282 ms 800 KB
subtask_01_06.txt RE 271 ms 800 KB
subtask_01_07.txt RE 278 ms 800 KB
subtask_01_08.txt RE 276 ms 792 KB
subtask_01_09.txt RE 276 ms 928 KB
subtask_01_10.txt RE 282 ms 796 KB
subtask_01_11.txt RE 277 ms 736 KB
subtask_01_12.txt RE 274 ms 672 KB
subtask_01_13.txt RE 273 ms 804 KB
subtask_01_14.txt RE 273 ms 800 KB
subtask_01_15.txt RE 278 ms 668 KB
subtask_01_16.txt RE 273 ms 804 KB
subtask_01_17.txt RE 282 ms 796 KB
subtask_01_18.txt RE 273 ms 676 KB