Pagini recente » Cod sursa (job #2811283) | Cod sursa (job #1579985) | Cod sursa (job #1583374) | Cod sursa (job #982495) | Cod sursa (job #1131197)
#include <stdio.h>
#define IN "disjoint.in"
#define OUT "disjoint.out"
#define NMAX 100005
int t[NMAX],h[NMAX];
int cod,x,y,i,n,m;
int gaseste(int x)
{
int alfa,y;
for(alfa=x;t[alfa]!=alfa;alfa=t[alfa]);
for(;t[x]!=x;)
{
y=t[x];
t[x]=alfa;
x=y;
}
return alfa;
}
void uneste(int x, int y)
{
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
if(h[x]==h[y])
h[y]++;
}
int main()
{
FILE * fin=fopen(IN,"r");
FILE * fout=fopen(OUT,"w");
fscanf(fin,"%d%d",&n,&m);
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
fscanf(fin,"%d%d%d",&cod,&x,&y);
if(cod==1)
uneste(gaseste(x),gaseste(y));
else
{
if(gaseste(x)==gaseste(y))
fprintf(fout,"DA\n");
else
fprintf(fout,"NU\n");
}
}
fclose(fin);
fclose(fout);
return 0;
}