Pagini recente » Cod sursa (job #1112068) | Cod sursa (job #1571740) | Cod sursa (job #2183579) | Cod sursa (job #1246468) | Cod sursa (job #1301525)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
vector <int> v;
int x, y, i, a, b, op, n, m;
int find_root(int x){
if(v[x]<0) return x;
else{
v[x] = find_root(v[x]);
return v[x];
}
}
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 main(){
fin >> n >> m;
for(i=0; i<=n; i++) v.push_back(-1);
for(i=1; i<=m; i++){
fin >> op >> x >> y;
a= find_root(y);
b= find_root(x);
if(op==1) quick_union(a, b);
else{
if(a==b) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}