Pagini recente » Cod sursa (job #2276392) | Cod sursa (job #992815) | Cod sursa (job #956805) | Cod sursa (job #2777382) | Cod sursa (job #1329775)
#include <iostream>
#include <fstream>
#define nmax 100005
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int v[nmax];
int n, m;
void quick_union(int x, int y){
if(v[x] > v[y]){
v[y]+= v[x];
v[x]= y;
}
else{
v[x]+= v[y];
v[y]= x;
}
}
int find_representative(int x){
if(v[x]<0) return x;
else{
v[x]= find_representative(v[x]);
return v[x];
}
}
int main(){
int op, x, y;
fin >> n >> m;
for(int i=1; i<=n; i++) v[i]= -1;
for(int i=1; i<=m; i++){
fin >> op >> x >> y;
int a= find_representative(x);
int b= find_representative(y);
if(op==1) quick_union(a, b);
else if(op==2 && a!=b) fout << "NU\n";
else fout << "DA\n";
}
return 0;
}