Cod sursa(job #2930468)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 28 octombrie 2022 15:21:26
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
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(){
 	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	fin >> n >> m;
	for(int i=1;i<=n;i++){
		parent[i]=i;
	}
	for(int i=1;i<=m;i++){
		int cod,x,y;
		fin >> cod >> x >> y;
		if(cod==1) merge(x,y);
		else{
			if(find(x)==find(y)) fout << "DA" << endl;
			else fout << "NU" << endl;	
		}
	}
 	return 0;
}