Cod sursa(job #2837332)

Utilizator LXGALXGA a LXGA Data 22 ianuarie 2022 09:52:43
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <algorithm>

using namespace std;

int t[100001],h[100001];
int n,m;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int f(int x)
{
	int aux=x;
	while(t[x]!=0)
		x=t[x];
	while(t[aux]!=0)
	{
		int xx=t[aux];
		t[aux]=x;
		aux=xx;
	}
	return x;
}

void u(int x,int y)
{
	int xx=f(x),yy=f(y);
	if(h[xx]>h[yy])
		t[yy]=xx;
	else if(h[xx]<h[yy])
		t[xx]=yy;
	else
	{
		t[yy]=xx;
		h[xx]++;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	int cod,x,y;
	for(int i=1;i<=m;i++)
	{
		cin>>cod>>x>>y;
		if(cod==1)
			u(x,y);
		else
		{
			if(f(x)==f(y))
				cout<<"DA\n";
			else cout<<"NU\n";
		}
	}	
	return 0;
}