Submission #423129


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;
      }

    }
  }
}

Submission Info

Submission Time
Task B - Union Find
User otomarukanta
Language C++ (GCC 4.9.2)
Score 0
Code Size 968 Byte
Status RE
Exec Time 293 ms
Memory 920 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 289 ms 736 KB
subtask_01_01.txt RE 293 ms 920 KB
subtask_01_02.txt RE 292 ms 788 KB
subtask_01_03.txt RE 287 ms 692 KB
subtask_01_04.txt RE 278 ms 804 KB
subtask_01_05.txt RE 279 ms 796 KB
subtask_01_06.txt RE 282 ms 800 KB
subtask_01_07.txt RE 290 ms 800 KB
subtask_01_08.txt RE 281 ms 796 KB
subtask_01_09.txt RE 280 ms 672 KB
subtask_01_10.txt RE 287 ms 800 KB
subtask_01_11.txt RE 280 ms 672 KB
subtask_01_12.txt RE 280 ms 804 KB
subtask_01_13.txt RE 279 ms 676 KB
subtask_01_14.txt RE 279 ms 800 KB
subtask_01_15.txt RE 279 ms 800 KB
subtask_01_16.txt RE 279 ms 668 KB
subtask_01_17.txt RE 283 ms 796 KB
subtask_01_18.txt RE 283 ms 744 KB