Submission #3404649
Source Code Expand
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long unsigned int ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define DEBUG
#ifdef DEBUG
#define var_dump(...) fprintf(stdout, __VA_ARGS__)
#define dump(a) cout << a << "\n";
#else
#define var_dump(...) 42
#define dump(a) 42
#endif
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};
// https://beta.atcoder.jp/contests/abc049/tasks/arc065_b
int par[200005];
int ranks[200005];
struct union_find {
vector<int> par, ranks;
// n要素で初期化
void init(int n) {
par.resize(n);
ranks.resize(n);
REP(i, n) {
par[i] = i;
ranks[i] = 0;
}
}
// 木の根を求める
int root(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
if (ranks[x] < ranks[y]) {
par[x] = y;
} else {
par[y] = x;
if (ranks[x] == ranks[y]) ranks[x]++;
}
}
// xとyが同じ集合に属するか
bool same(int x, int y) {
return root(x) == root(y);
}
};
int main() {
int N, Q; cin >> N >> Q;
int p, a, b;
union_find uf;
uf.init(N);
REP(i, Q) {
cin >> p >> a >> b;
if (p == 0) {
uf.unite(a, b);
} else {
cout << ((uf.same(a, b)) ? "Yes" : "No") << "\n";
}
}
}
Submission Info
Submission Time |
|
Task |
B - Union Find |
User |
wakamenod |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
2389 Byte |
Status |
AC |
Exec Time |
460 ms |
Memory |
1664 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
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 |
AC |
1 ms |
256 KB |
subtask_01_01.txt |
AC |
271 ms |
640 KB |
subtask_01_02.txt |
AC |
2 ms |
1024 KB |
subtask_01_03.txt |
AC |
412 ms |
1024 KB |
subtask_01_04.txt |
AC |
457 ms |
1664 KB |
subtask_01_05.txt |
AC |
24 ms |
256 KB |
subtask_01_06.txt |
AC |
26 ms |
1024 KB |
subtask_01_07.txt |
AC |
436 ms |
768 KB |
subtask_01_08.txt |
AC |
459 ms |
1664 KB |
subtask_01_09.txt |
AC |
1 ms |
256 KB |
subtask_01_10.txt |
AC |
2 ms |
1024 KB |
subtask_01_11.txt |
AC |
412 ms |
896 KB |
subtask_01_12.txt |
AC |
456 ms |
1664 KB |
subtask_01_13.txt |
AC |
357 ms |
768 KB |
subtask_01_14.txt |
AC |
3 ms |
1024 KB |
subtask_01_15.txt |
AC |
424 ms |
768 KB |
subtask_01_16.txt |
AC |
460 ms |
1664 KB |
subtask_01_17.txt |
AC |
307 ms |
1408 KB |
subtask_01_18.txt |
AC |
302 ms |
1408 KB |