Pagini recente » Cod sursa (job #308048) | Cod sursa (job #711751) | Cod sursa (job #2794956) | Cod sursa (job #1411312) | Cod sursa (job #2060044)
#include <stdio.h>
#include <stdlib.h>
#define Nmax 100004
int t[Nmax];
int h[Nmax];
int Find (int x)
{
if (t[x]!=0) return Find(t[x]);
return x;
}
void Union (int x,int y)
{
if (h[x]==h[y]) {t[x]=y;h[y]++;}
else
if (h[x]<h[y]) t[x]=y;
else t[y]=x;
}
int main()
{ int n,m,i,x,y,z;
FILE *f;
f=fopen("disjoint.in","r");
FILE *g;
g=fopen("disjoint.out","w");
fscanf(f,"%d %d",&n,&m);
for (i=1;i<=m;i++)
{
fscanf(f,"%d %d %d",&x,&y,&z);
int c1,c2;
c1=Find(y);
c2=Find(z);
if (x==2)
{
if (c1!=c2) fprintf(g,"NU \n");
else fprintf(g,"DA \n");
}
else
if (c1!=c2) Union(c1,c2);
}
fclose(f);
fclose(g);
return 0;
}