Cod sursa(job #780562)

Utilizator PatrikStepan Patrik Patrik Data 20 august 2012 19:51:14
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
	#include<stdio.h>
	#define MAX 100001
	FILE *f , *g ;
	int rang[MAX] , point[MAX]  , n , m , tip , x , y  ;
	
	
	int find(int x);
	void reuniune(int x , int y);
	
	int main()
	{
		f=fopen("disjoint.in" , "r" );
		g=fopen("disjoint.out" , "w" );
		fscanf(f , "%d%d" , &n , &m );
		for( int i = 1 ; i<= n ; ++i )
		{
			rang[i] = 1;
			point[i] = i;
		}
		for( int i = 1 ; i <= m ; ++i )
		{
			fscanf(f , "%d%d%d" , &tip , &x , &y) ;
			if(tip == 2)
				if(find(x) == find(y))
					fprintf(g , "DA\n");
				else
					fprintf(g , "NU\n");
			else
				reuniune(find(x) , find(y));
		}
		fclose(f);
		fclose(g);
		return 0;
	}
	
	int find(int x)
	{
		int aux , aux1;
		aux = point[x];
		while(aux != point[aux])
			aux = point[aux];
		while(x != point[x])
		{
			aux1 = point[x];
			point[x] = aux;
			x = aux1;
		}
		return aux;
	}
	
	void reuniune(int x , int y)
	{
		if(rang[x] > rang[y])
			point[y] = x;
		else
			point[x] = y;
		if(rang[x] == rang[y])
			rang[y]++;
	}