Cod sursa(job #2940867)

Utilizator SurtexXGheorghe Robert-Mihai SurtexX Data 16 noiembrie 2022 18:07:14
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int main()
{
	int n, m, cod, x, y;
	f >> n >> m;
	vector<vector<int>> adjacence_list(n+1, vector<int>(0));
	vector<int> root(n+1);
	for (int i = 1; i <= n; i++)
	{
		adjacence_list[i].push_back(i);
		root[i] = i;
	}
	for (int i = 1; i <= m; i++)
	{
		f >> cod >> x >> y;
		if (cod == 1) {
			adjacence_list[root[x]].push_back(root[y]);
			adjacence_list[root[y]].push_back(root[x]);
			for (int j = 0; j < adjacence_list[y].size(); j++)
				root[adjacence_list[y][j]] = root[x];
		}
		else
		{
			if (root[x] == root[y])
				g << "DA\n";
			else
				g << "NU\n";
		}
	}
	return 0;
}