Cod sursa(job #3317637)

Utilizator androcaOprea Andrei androca Data 24 octombrie 2025 18:02:18
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
using namespace std;

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

int sef[100000];
int bos (int x){
    if(sef[x] == x)
			   return x;
		else
			   return sef[x] = bos(sef[x]);
}

void uni(int x, int y)
{
	  int sefx = bos(x), sefy = bos(x);
	  sef[sefy] = sef[sefx];
}

int main()
{
		int n, m, c, x, y;
		for(int i = 1; i <= n; i++)
				sef[i] = i;
		cin >> n >> m;
		for(int i = 1; i <= m; i++){
				cin >> c >> x >> y;
				if (c == 1)
						unire(x, y);
				else{
						if (boss(x) == boss(y))
								cout << "DA" << '\n';
						else
								cout << "NU" << '\n';
		}
    return 0;
}