Cod sursa(job #3294322)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 21 aprilie 2025 13:58:23
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 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);
		v[i].reserve(n + 1);
	}

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

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

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