Cod sursa(job #3294324)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 21 aprilie 2025 14:05:13
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
//ht
// tps://infoarena.ro/problema/disjoint
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("fast-math")
//#pragma GCC optimize ("unroll-loops")
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

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

vector <int> v[100005];
int b[100005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, m, i, c, x, y;

	fin >> n >> m;

	for (i = 1; i <= n; ++i)
	{
		b[i] = i;
		v[i].push_back(i);
	}

	for (i = 1; i <= m; ++i)
	{
		fin >> c >> x >> y;

		if (c == 1)
		{
			int xx = b[x];
			int yy = b[y];
			if (v[xx].size() < v[yy].size())
				swap(xx, yy);

			for (auto it : v[yy])
			{
				b[it] = xx;
				v[xx].push_back(it);
			}
		}
		else
		{
			//cout << b[x] << " " << b[y] << "\n";
			if (b[x] == b[y])
				fout << "DA\n";
			else
				fout << "NU\n";
		}
	}
	return 0;
}