Cod sursa(job #276186)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 10 martie 2009 22:16:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include<stdio.h>
#define nmax 100010
long a[nmax], n, m;
FILE *f, *g;

long find(long x)
{	if(a[x]==x)
		return x;
	else
		find(a[x]);
}

int main()
{       long i, x, y, c, rx, ry;
	f=fopen("disjoint.in", "r");
	g=fopen("disjoint.out", "w");
	fscanf(f, "%ld%ld", &n, &m);
	for(i=1; i<=n; i++)
		a[i]=i;
	for(i=1; i<=m; i++)
	{	fscanf(f, "%ld%ld%ld", &c, &x, &y);
		rx=find(x);
		ry=find(y);
		if(c==1)
			a[rx]=ry;
		else
			if(rx==ry)     	fprintf(g, "DA\n");
			else            fprintf(g, "NU\n");

	}
	return 0;
}