Pagini recente » Cod sursa (job #1885122) | Cod sursa (job #2899827) | Cod sursa (job #2065987) | Cod sursa (job #1167221) | Cod sursa (job #2837332)
#include <fstream>
#include <algorithm>
using namespace std;
int t[100001],h[100001];
int n,m;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int f(int x)
{
int aux=x;
while(t[x]!=0)
x=t[x];
while(t[aux]!=0)
{
int xx=t[aux];
t[aux]=x;
aux=xx;
}
return x;
}
void u(int x,int y)
{
int xx=f(x),yy=f(y);
if(h[xx]>h[yy])
t[yy]=xx;
else if(h[xx]<h[yy])
t[xx]=yy;
else
{
t[yy]=xx;
h[xx]++;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>m;
int cod,x,y;
for(int i=1;i<=m;i++)
{
cin>>cod>>x>>y;
if(cod==1)
u(x,y);
else
{
if(f(x)==f(y))
cout<<"DA\n";
else cout<<"NU\n";
}
}
return 0;
}