Submission #420177


Source Code Expand

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<25;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;
#define MAX_N 100000
struct UF {
	int par[MAX_N],rank[MAX_N];
	
	void init(int n){
		for(int i = 0; i < n; i++){
			par[i] = i;
			rank[i] = 0;
		}
	}
	
	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}
	
	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;
		
		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = par[x];
			if(rank[x] == rank[y]) rank[y]++;
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
};


int main(){
	int n, q;
	cin>>n>>q;
	UF uf;
	uf.init(n);
	for(int i = 0; i < q; i++){
		int p, a, b;
		cin>>p>>a>>b;
		if(p==0) uf.unite(a, b);
		else {
			cout<<(uf.same(a, b)?"Yes":"No")<<endl;
		}
	}
	return 0;
}

Submission Info

Submission Time
Task B - Union Find
User Lepton
Language C++ (GCC 4.9.2)
Score 100
Code Size 1341 Byte
Status AC
Exec Time 869 ms
Memory 1696 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
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 24 ms 916 KB
subtask_01_01.txt AC 481 ms 924 KB
subtask_01_02.txt AC 27 ms 1560 KB
subtask_01_03.txt AC 690 ms 808 KB
subtask_01_04.txt AC 828 ms 1572 KB
subtask_01_05.txt AC 68 ms 924 KB
subtask_01_06.txt AC 72 ms 1568 KB
subtask_01_07.txt AC 749 ms 924 KB
subtask_01_08.txt AC 869 ms 1572 KB
subtask_01_09.txt AC 24 ms 796 KB
subtask_01_10.txt AC 26 ms 1564 KB
subtask_01_11.txt AC 711 ms 928 KB
subtask_01_12.txt AC 829 ms 1568 KB
subtask_01_13.txt AC 638 ms 808 KB
subtask_01_14.txt AC 28 ms 1696 KB
subtask_01_15.txt AC 713 ms 928 KB
subtask_01_16.txt AC 857 ms 1696 KB
subtask_01_17.txt AC 630 ms 1564 KB
subtask_01_18.txt AC 635 ms 1572 KB