Submission #6903576
Source Code Expand
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <fstream>
#include<cstdio>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include <cstdlib>
#include <typeinfo>
#include <math.h>
#define REP(i, n) for(int i=0;i<n;i++)
#define REP2(i, s, n) for(int i=s;i<n;i++)
#define REP_1(i, n) for(int i=1;i<n+1;i++)
#define bitSearch(bit, n) for(int bit = 0; bit < (1 << n); bit++)
using namespace std;
template<class T>
void print(const T &value) {
std::cout << value << std::endl;
}
void yesno(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; }
void YesNo(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; }
void YESNO(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; }
typedef long long ll;
typedef unsigned long ul;
typedef long double ld;
template<class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll INF = 10000000;
ll mod = 1000000007;//10^9+7
int dx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int dy[8] = {0, -1, 1, 0, -1, 1, -1, 1};
using Graph = vector<vector<int>>;//隣接リスト:G[i]にはiと隣接する頂点が入るよ。
using P = pair<int, int>;//BFSで利用。queueに入れる。
using lP = pair<ll, ll>;
using PP = pair<int, P>; //dijkstraで利用,priority_queueに入れる。
using p_queue = priority_queue<int, vector<int>, greater<int>>;//小さい順
//struct edge{int to, cost};
int max_N = 100000;
// Union Find木の実装
int par[100000];
int rnk[100000];
//n個のnodeで初期化する関数
void init(int n) {
REP(i, n) {
par[i] = i;
rnk[i] = 0;
}
}
//木の根を求める。この際,親は根に更新される。
int find(int x) {
if (par[x] == x) { return x; }
else { return par[x] = find(par[x]); }
}
//xとyの属する集合を融合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) { return; }
if (rnk[x] < rnk[y]) {
par[x] = y;
} else {
par[y] = x;
if (rnk[x] == rnk[y]) { rnk[x]++; }
}
}
//xとyが同じ集合に属するか判定
bool same(int x, int y) {
return find(x) == find(y);
}
//番号ズレ注意!!
int main() {
int N, Q;
cin >> N >> Q;
init(N);
REP(i, Q) {
int P, A, B;
cin >> P >> A >> B;
if (P == 0) {
unite(A, B);
}
if (P == 1) {
YesNo(same(A, B));
}
}
}
Submission Info
Submission Time |
|
Task |
B - Union Find |
User |
Matsumatsumatsu |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
2746 Byte |
Status |
AC |
Exec Time |
501 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 |
288 ms |
640 KB |
subtask_01_02.txt |
AC |
2 ms |
1024 KB |
subtask_01_03.txt |
AC |
431 ms |
1152 KB |
subtask_01_04.txt |
AC |
500 ms |
1664 KB |
subtask_01_05.txt |
AC |
26 ms |
256 KB |
subtask_01_06.txt |
AC |
29 ms |
1024 KB |
subtask_01_07.txt |
AC |
455 ms |
896 KB |
subtask_01_08.txt |
AC |
495 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 |
430 ms |
896 KB |
subtask_01_12.txt |
AC |
501 ms |
1664 KB |
subtask_01_13.txt |
AC |
374 ms |
768 KB |
subtask_01_14.txt |
AC |
3 ms |
1024 KB |
subtask_01_15.txt |
AC |
437 ms |
896 KB |
subtask_01_16.txt |
AC |
492 ms |
1664 KB |
subtask_01_17.txt |
AC |
344 ms |
1408 KB |
subtask_01_18.txt |
AC |
341 ms |
1408 KB |