Pagini recente » Cod sursa (job #2396906) | Cod sursa (job #1040908) | Cod sursa (job #66547) | Cod sursa (job #2586984) | Cod sursa (job #2077305)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 100002;
int root[NMAX],ranks[NMAX];
int find_father(int x)
{
if(root[x]==x)
return x;
x=find_father(root[x]);
root[x]=x;
return x;
}
void unite(int x,int y)
{
int rootx=find_father(x);
int rooty=find_father(y);
if(ranks[rootx]>ranks[rooty])
root[rooty]=rootx;
else if(ranks[rooty]>ranks[rootx])
root[rootx]=rooty;
else
{
root[rootx]=rooty;
ranks[rooty]++;
}
}
void querry(int x,int y)
{
if(find_father(x)==find_father(y))
out<<"YES"<<'\n';
else
out<<"NO"<<'\n';
}
int main()
{
int nodes,t;
in>>nodes>>t;
for(int i=1; i<=nodes; i++)
{
root[i]=i;
}
for(int i=1; i<=t; i++)
{
int p,x,y;
in>>p>>x>>y;
if(p==1)
unite(x,y);
else
querry(x,y);
}
}