Pagini recente » Cod sursa (job #2433978) | Cod sursa (job #3160367) | Cod sursa (job #2748837) | Cod sursa (job #2611241) | Cod sursa (job #2945603)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int> tata(100001), rang(100001);
int N, M;
int findAncestor(int k){
///we recursively traverse the fathers vector to find the one node that doesn't have a father
if(tata[k]) {
tata[k] = findAncestor(tata[k]);
return tata[k];
}
return k;
}
int main(){
f >> N >> M;
int op, x, y;
while (M--){
f >> op >> x >> y;
if(op == 1){
if(findAncestor(x) != findAncestor(y)){
tata[findAncestor(x)] = findAncestor(y);
}
}
else{
if(findAncestor(x) == findAncestor(y))
g << "DA\n";
else
g << "NU\n";
}
}
f.close();
g.close();
return 0;
}