Submission #420224


Source Code Expand

#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
struct unionf{
	vector <int> data;
	unionf(int size):data(size,-1){}
	bool unions(int x,int y){
		x=root(x);y=root(y);
		if(x!=y){
			if(data[y]<data[x]){int d=x;x=y;y=d;}
			data[x]+=data[y];data[y]=x;
		}
		return x!=y;
	}
	bool finds(int x,int y){
		return root(x)==root(y);
	}
	int root(int x){
		return data[x]<0?x:data[x]=root(data[x]);
	}
	int find(int x){
		return -data[x];
	}
};
int main()
{
	int n,q,p,a,b;
	cin>>n>>q;
	unionf uni(n+10);
	rep(i,q){
		cin>>p>>a>>b;
		if(p==0) uni.unions(a,b);
		if(p==1){
			if(uni.finds(a,b)) cout<<"Yes"<<endl;
			else cout<<"No"<<endl;
		}
	}
}

Submission Info

Submission Time
Task B - Union Find
User sky58
Language C++ (GCC 4.9.2)
Score 100
Code Size 1321 Byte
Status AC
Exec Time 993 ms
Memory 1316 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 23 ms 924 KB
subtask_01_01.txt AC 508 ms 920 KB
subtask_01_02.txt AC 25 ms 1308 KB
subtask_01_03.txt AC 883 ms 924 KB
subtask_01_04.txt AC 972 ms 1312 KB
subtask_01_05.txt AC 64 ms 916 KB
subtask_01_06.txt AC 70 ms 1184 KB
subtask_01_07.txt AC 709 ms 920 KB
subtask_01_08.txt AC 885 ms 1176 KB
subtask_01_09.txt AC 28 ms 928 KB
subtask_01_10.txt AC 27 ms 1316 KB
subtask_01_11.txt AC 667 ms 920 KB
subtask_01_12.txt AC 993 ms 1312 KB
subtask_01_13.txt AC 759 ms 804 KB
subtask_01_14.txt AC 29 ms 1184 KB
subtask_01_15.txt AC 905 ms 928 KB
subtask_01_16.txt AC 817 ms 1188 KB
subtask_01_17.txt AC 653 ms 1304 KB
subtask_01_18.txt AC 682 ms 1128 KB