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

Submission #7128638

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;
vector<int> rank;

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

if (rank[a] < rank[b]) {
parent[a] = b;
}
else {
parent[b] = a;
if (rank[a] == rank[b]) ++rank[a];
}
}
bool isSameGroup(int a, int b) {
return getRoot(a) == getRoot(b);
}
};

int getRoot(vector<int> &lines, int v) {
return lines[v] == v ? v : lines[v] = getRoot(lines, lines[v]);
}

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();
// cout << lines[a] << " " << lines[b] << endl;
}
}
}
```

#### Submission Info

Submission Time 2019-08-25 10:37:58+0900 B - Union Find scientistb C++14 (GCC 5.4.1) 100 1837 Byte AC 492 ms 1664 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