Submission #420708


Source Code Expand

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

class UnionFindTree{
	typedef struct {
		int parent;
		int rank;
	}base_node;
	
	vector<base_node> node;
public:
	UnionFindTree(int n){
		node.resize(n);
		for(int i=0; i<n; i++){
			node[i].parent=i;
			node[i].rank=0;
		}
	}

	int find(int x){
		if(node[x].parent == x) return x;
		else{
			return node[x].parent = find(node[x].parent);
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		x = find(node[x].parent);
		y = find(node[y].parent);
		if(x==y) return;
		if(node[x].rank < node[y].rank){
			node[x].parent = y;
		}else if(node[x].rank > node[y].rank){
			node[y].parent = x;
		}else{
			node[x].rank++;
			unite(x,y);
		}
	}
};


int main(){
	int n,q;
	cin >> n >> q;
	UnionFindTree uft(n);

	while(q--){
		int p,a,b;
		cin >> p >> a >> b;
		if(p==0){
			uft.unite(a,b);
		}else{
			cout << (uft.same(a,b)?"Yes":"No") << endl;
		}
	}

	return 0;
}

Submission Info

Submission Time
Task B - Union Find
User koyumeishi
Language C++11 (GCC 4.9.2)
Score 100
Code Size 1174 Byte
Status AC
Exec Time 861 ms
Memory 1700 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 28 ms 796 KB
subtask_01_01.txt AC 508 ms 928 KB
subtask_01_02.txt AC 25 ms 1692 KB
subtask_01_03.txt AC 679 ms 736 KB
subtask_01_04.txt AC 861 ms 1564 KB
subtask_01_05.txt AC 63 ms 748 KB
subtask_01_06.txt AC 73 ms 1560 KB
subtask_01_07.txt AC 777 ms 800 KB
subtask_01_08.txt AC 841 ms 1572 KB
subtask_01_09.txt AC 25 ms 924 KB
subtask_01_10.txt AC 26 ms 1700 KB
subtask_01_11.txt AC 685 ms 676 KB
subtask_01_12.txt AC 839 ms 1700 KB
subtask_01_13.txt AC 625 ms 804 KB
subtask_01_14.txt AC 30 ms 1576 KB
subtask_01_15.txt AC 675 ms 732 KB
subtask_01_16.txt AC 847 ms 1696 KB
subtask_01_17.txt AC 624 ms 1568 KB
subtask_01_18.txt AC 616 ms 1696 KB