Cod sursa(job #2866643)

Utilizator TudosieRazvanTudosie Marius-Razvan TudosieRazvan Data 9 martie 2022 21:11:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb

#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <unordered_map>

#define NMAX 100003


using namespace std;

int n, m;
int tata[NMAX];



ifstream fin("disjoint.in");
ofstream fout("disjoint.out");



int cRad(int k)
{
	int rad = k;
	while (tata[rad] > 0)
	{
		rad = tata[rad];
	}
	int nr = k;
	while (tata[nr] > 0)
	{
		int aux = nr;
		nr = tata[nr];
		tata[aux] = rad;
	}
	return rad;
}

int main()
{
	fin >> n >> m;

	for (int i = 1; i <= n; i++)
	{
		tata[i] = -1;
	}

	for (int i = 1; i <= m; i++)
	{
		int op, x, y;
		fin >> op >> x >> y;
		int rad1 = cRad(x);
		int rad2 = cRad(y);
		if (op == 1)
		{
			//reuniune
			if (rad1 != rad2)
			{
				if (tata[rad1] <= tata[rad2])
				{
					tata[rad1] += tata[rad2];
					tata[rad2] = rad1;
				}
				else {
					tata[rad2] += tata[rad1];
					tata[rad1] = rad2;
				}
			}
		}
		else {
			if (rad1 == rad2)
			{
				fout << "DA\n";
			}
			else {
				fout << "NU\n";
			}
		}

	}

	


	
	return 0;
}