Cod sursa(job #2552461)

Utilizator EltMenimTirisi Claudiu EltMenim Data 20 februarie 2020 20:56:03
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("disjoint.in");
ofstream o("disjoint.out");

int father(int x, vector<int>&tata){
int t=tata[x];
while(t!=tata[t]) t=tata[t];
return t;
}

void op1(int x, int y, vector<int>&tata){
int tx=father(x, tata), ty=father(y, tata);
tata[tx]=ty;
}

void op2(int x, int y, vector<int>&tata){
if(father(x, tata)==father(y, tata)){
o<<"DA"<<'\n';
} else o<<"NU"<<'\n';
}

int main(){
int vf, ops, op, x, y;
f>>vf>>ops;
vector<int> tata(vf, 0);

for(int i=0;i<vf;i++) tata[i]=i;

while(ops--){
f>>op>>x>>y;
x--;
y--;
if(op==1) op1(x, y, tata);
if(op==2) op2(x, y, tata);
}

}