Pagini recente » Cod sursa (job #748649) | Cod sursa (job #822309) | Cod sursa (job #2594356) | Cod sursa (job #1641786) | Cod sursa (job #2930584)
#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector<int> father,depth;
int n,m;
int root(int node){
while(father[node]){
node=father[node];
}
return node;
}
void merge(int a,int b){
if(depth[a]==depth[b]){
father[a]=b;
depth[b]++;
}
else if(depth[a]<depth[b]){
father[a]=b;
}
else{
father[b]=a;
}
}
void solve(){
in>>n>>m;
father.resize(n+1);
depth.resize(n+1);
int a,b,c;
while(m--){
in>>a>>b>>c;
int first=root(b),second=root(c);
if(a==1){
merge(first,second);
}
else{
cout<<(first==second?"DA":"NU")<<'\n';
}
}
}
int main(){
solve();
return 0;
}