Pagini recente » Cod sursa (job #890481) | Cod sursa (job #3225299) | Cod sursa (job #1206001) | Cod sursa (job #1815702) | Cod sursa (job #1265385)
#include <cstdio>
using namespace std;
int n,m,t[100001],h[100001],o,x,y,x1,y1,i,r1,r2,c;
int main()
{FILE *f1 = fopen("disjoint.in","r");
FILE *f2 = fopen("disjoint.out","w");
fscanf(f1,"%d%d",&n,&m);
for (i=1;i<=n;i++) {h[i]=1;t[i]=i;}
for (i=1;i<=m;i++)
{fscanf(f1,"%d%d%d",&o,&x,&y);
r1=x;r2=y;
while (r1!=t[r1]) r1=t[r1];
while (r2!=t[r2]) r2=t[r2];
if (o==1)
{if (h[r1]>h[r2]) {t[r2]=r1;c=r1;}
else {t[r1]=r2;c=r2;}
if (h[r1]==h[r2]) h[c]++;
}
else
{if (r1==r2) fprintf(f2,"DA\n");
else fprintf(f2,"NU\n");
while (t[x]!=r1) {x1=t[x];t[x]=t[x1];x=x1;}
while (t[y]!=r2) {y1=t[y];t[y]=t[y1];y=y1;}
}
}
fclose(f1);
fclose(f2);
return 0;
}
//Challenges are what make life interesting and overcoming them is what makes life meaningful.