Cod sursa(job #353214)

Utilizator IoannaPandele Ioana Ioanna Data 4 octombrie 2009 14:26:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include<stdio.h>
#define nmax 100005
long n,m;
long v[nmax];

struct nod
{
	long info;
	nod *urm;
};

nod *t[nmax];

void read()
{
	scanf("%ld%ld",&n,&m);
}
 
void init()
{
	long i;
	nod *aux;
	for (i=1;i<=n;i++)
	{
		aux=new nod;
		aux->info=i;
		aux->urm=NULL;
		t[i]=aux;
		v[i]=i;
	}
	
}

void connect(long a,long b)
{
	nod *p,*aux;
	long r,s;
	r=v[a];
    s=v[b];
	p=t[s];
	while (p)
	{
		aux=p;
		t[s]=p->urm;
		p=t[s];
		v[aux->info]=r;
		aux->urm=t[r];
		t[r]=aux;
	}
}

void rez()
{
	long i,a,b,c;
	for (i=1;i<=m;i++)
	{
		scanf("%ld%ld%ld",&c,&a,&b);
		if (c==1)
			if (v[a]<v[b])
				connect(a,b);
			else connect(b,a);
		else 
			if (v[a]==v[b])
				printf("DA\n");
			else printf("NU\n");
	}
		
}

int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	read();
	init();
	rez();
	return 0;
}