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

Submission #7125573

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

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

int main() {
int N, Q; cin >> N >> Q;
vector<int> lines(N);
REP(i, N) lines[i] = i;

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

// conbine
if (p == 0) {
lines[max(a, b)] = getRoot(lines, min(a, b));
}
// judge
if (p == 1) {
if (getRoot(lines, a) == getRoot(lines, b)) sayYes();
else sayNo();
// cout << lines[a] << " " << lines[b] << endl;
}
}
}

#### Submission Info

Submission Time 2019-08-25 01:46:44+0900 B - Union Find scientistb C++14 (GCC 5.4.1) 0 1312 Byte WA 486 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