Pagini recente » Cod sursa (job #2827859) | Cod sursa (job #1480062) | Cod sursa (job #266207) | Cod sursa (job #350727) | Cod sursa (job #2552461)
#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);
}
}