Submission #3049341


Source Code Expand

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_N 10000

int par[MAX_N]; // 親
int depth[MAX_N]; // 木の深さ

// n要素で初期化
void init(int n) {
	rep(i, 0, n) {
		par[i] = i;
		depth[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(depth[x] < depth[y]) {
		par[x] = y;
	} else {
		par[y] = x;
		if(depth[x] == depth[y]) depth[x]++;
	}
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) {
	return find(x) == find(y);
}


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, Q;
	cin >> N >> Q;
	init(N);
	rep(q, 0, Q) {
		int p, a, b;
		cin >> p >> a >> b;
		if(p == 0) unite(a, b);
		else {
			if(same(a, b)) cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	}
}

Submission Info

Submission Time
Task B - Union Find
User tsutarou10
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1714 Byte
Status RE
Exec Time 355 ms
Memory 1024 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 1
AC × 9
RE × 10
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 207 ms 640 KB
subtask_01_02.txt RE 109 ms 384 KB
subtask_01_03.txt AC 329 ms 1024 KB
subtask_01_04.txt RE 95 ms 384 KB
subtask_01_05.txt AC 19 ms 256 KB
subtask_01_06.txt RE 96 ms 384 KB
subtask_01_07.txt AC 333 ms 896 KB
subtask_01_08.txt RE 95 ms 384 KB
subtask_01_09.txt AC 1 ms 256 KB
subtask_01_10.txt RE 96 ms 384 KB
subtask_01_11.txt AC 355 ms 896 KB
subtask_01_12.txt RE 97 ms 384 KB
subtask_01_13.txt AC 267 ms 768 KB
subtask_01_14.txt RE 96 ms 384 KB
subtask_01_15.txt AC 333 ms 896 KB
subtask_01_16.txt RE 96 ms 384 KB
subtask_01_17.txt RE 96 ms 384 KB
subtask_01_18.txt RE 96 ms 384 KB