Pagini recente » Cod sursa (job #3323883) | Cod sursa (job #3344730) | Cod sursa (job #2854783) | Cod sursa (job #1047465) | Cod sursa (job #3328563)
#include <bits/stdc++.h>
using namespace std;
int father[100005];
int height[100005];
int rad(int node)
{
int root=node;
while(root!=father[root])
root=father[root];
father[node]=root;
int tmp=node,inplus;
while(tmp!=root)
{
inplus=father[tmp];
father[tmp]=root;
tmp=inplus;
}
return root;
}
void join(int x, int y)
{
int node1=rad(x);
int node2=rad(y);
if(height[node1]>height[node2])
father[node2]=node1;
else if(height[node1]<height[node2])
father[node1]=node2;
else
{
father[node2]=node1;
height[node1]++;
}
}
bool bueno(int x, int y)
{
if(rad(x)==rad(y))
return 1;
return 0;
}
int main()
{
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,tip,x,y;
cin>>n>>m;
for(int i=1;i<=n;i++)
father[i]=i,height[i]=1;
for(int i=0;i<m;i++)
{
cin>>tip>>x>>y;
if(tip==1)
{
join(x,y);
}
else
{
if(bueno(x,y)==0)
cout<<"NU"<<'\n';
else
cout<<"DA"<<'\n';
}
}
return 0;
}