Contest Duration: ~ (local time) (90 minutes) Back to Home

Submission #7129306

Source Code Expand

Copy
```#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <functional>
#include <string.h>
#include <numeric>
#include <math.h>

#define LOOP(N) for(int i=0; i<(N); ++i)
#define REP(i, N) for(int i=0; i<(N); ++i)
#define FOR(i, start, end) for(int i=(start); i<(end); ++i)
#define ALL(a) (a).begin(),(a).end()

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector< vector<int> >;

void sayYes() {cout << "Yes" << endl;}
void sayNo() {cout << "No" << endl;}

class UnionFind {
vector<int> parent;

int rank(int v) {
return -parent[v];
}
public:
UnionFind(int N) : parent(N, -1) {}
int getRoot(int v) {
return parent[v] < 0 ? v : parent[v] = getRoot(parent[v]);
}
void unite(int a, int b) {
a = getRoot(a);
b = getRoot(b);
if (a == b) return;

if (rank(a) < rank(b)) swap(a, b);
parent[a] += parent[b];
parent[b] = a;
}
bool isSameGroup(int a, int b) {
return getRoot(a) == getRoot(b);
}
void print(int a, int b) {
cout << a << " " << parent[a] << " " << b << " " << parent[b] << endl;
}
};

int main() {
int N, Q; cin >> N >> Q;
UnionFind unf(N);

int p, a, b;
REP(i, Q) {
cin >> p >> a >> b;

// conbine
if (p == 0) {
unf.unite(a, b);
}
// judge
if (p == 1) {
if (unf.isSameGroup(a, b)) sayYes();
else sayNo();
// unf.print(a, b);
}
}
}
```

#### Submission Info

Submission Time 2019-08-25 11:32:16+0900 B - Union Find scientistb C++14 (GCC 5.4.1) 100 1734 Byte AC 458 ms 1280 KB

#### Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 00_sample_01.txt
Case Name Status Exec Time Memory
00_sample_01.txt 1 ms 256 KB