Pagini recente » Cod sursa (job #2591938) | Cod sursa (job #860043) | Cod sursa (job #1102483) | Cod sursa (job #638165) | Cod sursa (job #2552466)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("disjoint.in");
ofstream o("disjoint.out");
int root(int x, vector<int>&tata){
int t=tata[x];
while(t!=tata[t]) t=tata[t];
tata[x]=t;
while(x!=tata[x]){
x=tata[x];
tata[x]=t;
}
return t;
}
void op1(int x, int y, vector<int>&tata){
int tx=root(x, tata), ty=root(y, tata);
tata[tx]=ty;
}
void op2(int x, int y, vector<int>&tata){
if(root(x, tata)==root(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);
}
}