Cod sursa(job #3317614)

Utilizator GoreaRaresGorea Rares-Andrei GoreaRares Data 24 octombrie 2025 17:33:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, m, t, x, y, sef[100001];

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

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

bool together(int x, int y){
	return boss(x) == boss(y);
}

void read(){
	fin >> n >> m;
	for(int i = 1; i <= n; i++){
		sef[i] = i;
	}
	for(int i = 1; i <= m; i++){
		fin >> t >> x >> y;
        if(t == 1){
        	unite(x, y);
        }
        else{
        	if(together(x, y)){
				fout << "DA\n";
        	}
        	else{
				fout << "NU\n";
        	}
        }
	}
}

int main()
{
	read();
    return 0;
}