Cod sursa(job #2837368)

Utilizator Langa_bLanga Radu Langa_b Data 22 ianuarie 2022 10:10:59
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
//ifstream cin("disjoint.in");
//ofstream cout("disjoint.out");
int n, m;
struct hey {
	int c;
	int x;
	int y;
};
int root[100002];
vector <int> vrez;
int radacina(int a) {
	if (a == root[a]) {
		return a;
	}
	else {
		return root[a] = radacina(root[a]);
	}
}
int main()
{
	cin >> n >> m;
	int c, x, y;
	int aux = n, cnt = 0;
	root[0] = 0;
	for (int i = 1; i <= m; i++) {
		cin >> c >> x >> y;
		if (c == 1) {
			if (root[x] == 0 && root[y] == 0) {
				root[x] = x;
				root[y] = x;
				aux--;
				
			}
			else if (root[x] == 0 && root[y] != 0) {
				root[x] = root[y];
				aux--;
			
			}
			else if (root[x] != 0 && root[y] == 0) {
				root[y] = root[x];
				aux--;
				
			}
			else {
				if (root[x] == root[y]) {
					
				}
				else {
					int interm1 = radacina(x);
					int interm2 = radacina(y);
					if (interm1 == interm2) {
						
					}
					else {
						aux--;
						if (interm2 > interm1) {
							root[interm2] = root[interm1];
						}
						else {
							root[interm1] = root[interm2];
						}
						
					}
				}
			}
		}
		else {
			int interm1 = radacina(x);
			int interm2 = radacina(y);
			if (interm1 == interm2 && interm1 != 0) {
				vrez.emplace_back(1);
			}
			else {
				vrez.emplace_back(0);
			}
		}
	}
	for (int i = 0; i < vrez.size(); i++) {
		if (vrez[i] == 0) {
			cout << "NU" << '\n';
		}
		else {
			cout << "DA" << '\n';
		}
	}
}