Pagini recente » Cod sursa (job #2260676) | Cod sursa (job #1844925) | Cod sursa (job #408184) | Cod sursa (job #33508) | Cod sursa (job #281015)
Cod sursa(job #281015)
#include<stdio.h>
#define Nmax 100100
long n,m,t[Nmax],gr[Nmax];
int cauta(long q)
{
long i,j;
for(i=q;i!=t[i];i=t[i]);
while(q!=t[q])
{ j=t[q];
t[q]=i;
q=j;
}
return i;
}
void lipeste(long x,long y)
{
if(gr[x]>gr[y]) t[y]=x;
else t[x]=y;
if(gr[x]==gr[y]) gr[y]++;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%ld%ld",&n,&m);
long x,y,cod,i;
for(i=1;i<=n;i++)
{ t[i]=i;
gr[i]=1;
}
for(i=1;i<=m;i++)
{ scanf("%ld%ld%ld",&cod,&x,&y);
if(cod==1)
lipeste(cauta(x),cauta(y));
else
if(cauta(x)==cauta(y)) printf("DA\n");
else printf("NU\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}