Pagini recente » Cod sursa (job #2344486) | Cod sursa (job #2621184) | Cod sursa (job #1018871) | Cod sursa (job #2572349) | Cod sursa (job #2934532)
#include<iostream>
#include<fstream>
#include<deque>
#include<algorithm>
#include<vector>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector<int> father,depth;
// Merge the two sets.
// A and B must be roots.
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;
}
}
// Get the set of the node.
int root(int node){
while(father[node]!=0){
node=father[node];
}
return node;
}
void solve(){
int n,m,c,x,y;
in>>n>>m;
father.resize(n+1,0);
depth.resize(n+1,0);
for(int i=0;i<m;i++){
in>>c>>x>>y;
if(c==1){
merge(root(x),root(y));
}
else{
out<<(root(x)==root(y)?"DA":"NU")<<'\n';
}
}
}
int main(){
solve();
return 0;
}