Cod sursa(job #2427413)

Utilizator leo281099Ionescu Leonard Octavian leo281099 Data 31 mai 2019 19:58:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100100
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector <pair <int, int> > graf[NMAX];
int grad[NMAX];
int tata[NMAX];
int n, m;
int gasesteTata(int nod)
{
	if (tata[nod] == nod) return nod;
	tata[nod] = gasesteTata(tata[nod]);
	return tata[nod];
}
void combina(int x, int y)
{
	int tx, ty;
	tx = gasesteTata(x);
	ty = gasesteTata(y);
	if (tx != ty)
	{
		if (grad[tx] >= grad[ty])
		{
			tata[ty] = tx;
			grad[tx] = grad[tx] + grad[ty];
		}
		else
		{
			tata[tx] = ty;
			grad[ty] = grad[ty] + grad[tx];
		}
	}
}
int main()
{
	int i, a, b, c;
	f >> n >> m;
	for (i = 1; i <= n; i++)
	{
		tata[i] = i;
		grad[i] = 0;
	}
	for (i = 1; i <= m; i++)
	{
		f >> a >> b >> c;
		if (a == 1)
			combina(b, c);
		else
			if (gasesteTata(b) == gasesteTata(c))
				g << "DA" << "\n";
			else
				g << "NU" << "\n";
	}
	return 0;
}