Cod sursa(job #2930463)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 28 octombrie 2022 15:15:30
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
#define ll long long
#define cin fin
#define cout fout
using namespace std;
const int maxn=1e5+1;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
vector<int>parent(maxn),rang(maxn);
 int find(int node){
 	if(parent[node]==node){
 		return node;
	}
	else return find(parent[node]);
 }
 void merge(int x,int y){
 	int xset= find(x);
 	int yset= find(y);
	parent[yset]=xset;
 }
 int main(){
	int n,m;
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		parent[i]=i;
	}
	for(int i=1;i<=m;i++){
		int cod,x,y;
		cin >> cod >> x >> y;
		if(cod==1) merge(x,y);
		else{
			if(find(x)==find(y)) cout << "DA" << endl;
			else cout << "NU" << endl;	
		}
	}
 	return 0;
}