Pagini recente » Cod sursa (job #3255909) | Cod sursa (job #1853314) | Cod sursa (job #3323505) | Cod sursa (job #3338186) | Cod sursa (job #3331077)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector<int>T(100001);
//aici ca sa ne miscam mai rapid,gen am un drum lung si ca sa nu mai merg mereu pe drumu ala leg direct de root tot
int get_root(int nod){
int aux=nod;
while(T[nod]>0){
nod=T[nod];
}
int root=nod;
nod=aux;//acu ma reintorc si in timp ce urc leg de root
//gen eu acu am urcat pana la root cu nod,de aia am zis root=nod
while(nod!=root){
aux=T[nod];
T[nod]=root;
nod=aux;
}
return root;
}
void join(int x,int y){
int root_x=get_root(x);
int root_y=get_root(y);
if(root_x==root_y){
return;
}
if(T[root_x]<=T[root_y]){
T[root_x]+=T[root_y];
T[root_y]=root_x;
}else{
T[root_y]+=T[root_x];
T[root_x]=root_y;
}
}
bool query(int x,int y){
return(get_root(x)==get_root(y));
}
int main(){
ios_base::sync_with_stdio(0);
in.tie(0);
out.tie(0);
int n,m,x,y,op;
in>>n>>m;
for(int i=1;i<=n;i++){
T[i]=-1;
}
for(int i=1;i<=m;i++){
in>>op>>x>>y;
if(op==1){
join(x,y);
}else{
if(query(x,y)){
out<<"DA"<<"\n";
}else{
out<<"NU"<<"\n";
}
}
}
}