https://www.acmicpc.net/problem/1717
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
//집합의 표현 1717번
vector<int> v; //(본인정보, 부모정보)
int my_find(int me)
{
if(me == v[me])
{
return me;
}
return v[me] = my_find(v[me]);
}
void my_union(int a, int b)
{
int A = my_find(a);
int B = my_find(b);
if(A == B)
{
return;
}
v[A] = B;
}
int main(void)
{
int n,m;
int q, from, to;
scanf("%d %d", &n, &m);
v.push_back(0);
for(int i=1; i<=n; i++)
{
v.push_back(i);
}
for(int i=0; i<m; i++)
{
scanf("%d %d %d", &q, &from, &to);
if(q == 0)
{
my_union(from, to);
}
else if(q==1)
{
if(my_find(from) == my_find(to))
{
printf("YES \n");
}
else
{
printf("NO \n");
}
}
}
return 0;
}