Pagini recente » Cod sursa (job #645736) | Cod sursa (job #2947422) | Cod sursa (job #447244) | Cod sursa (job #2106127) | Cod sursa (job #477986)
Cod sursa(job #477986)
#include<cstdio>
#include<vector>
using namespace std;
const int N=100001;
int n, rang[N], root[N];
int GetRoot(int i)
{
if(root[i]!=i)
root[i]=GetRoot(root[i]);
return root[i];
}
void Read()
{
int m, type, a, b, ra, rb;
scanf( "%d%d", &n, &m);
for( int i=1; i<=n; ++i)
rang[i]=1, root[i]=i;
while(m--)
{
scanf( "%d%d%d", &type, &a, &b);
if(type==1)
{
ra=GetRoot(a);
rb=GetRoot(b);
if(ra!=rb)
{
if(rang[ra]<rang[rb])
{
root[ra]=rb;
rang[rb]+=rang[ra];
}
else
{
root[rb]=ra;
rang[ra]+=rang[rb];
}
}
}
else
{
ra=GetRoot(a);
rb=GetRoot(b);
if(ra==rb)
printf("DA\n");
else
printf("NU\n");
}
}
}
int main()
{
freopen( "disjoint.in", "r", stdin);
freopen( "disjoint.out", "w", stdout);
Read();
return 0;
}