Pagini recente » Cod sursa (job #2114358) | Cod sursa (job #2542166) | Cod sursa (job #1897312) | Cod sursa (job #1926707) | Cod sursa (job #2408857)
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
map<int,int> fatherOf;
map<int,int> grade;
int findFather(int node){
if(fatherOf[node] == node){
return node;
}
fatherOf[node] = findFather(fatherOf[node]);
return fatherOf[node];
}
int main() {
int n,m,x,y,A,B,o;
freopen("disjoint.in","r", stdin);
freopen("disjoint.out", "w", stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
fatherOf[i] = i;
grade[i] = 1;
}
for(int i=1;i<=m;i++){
cin>>o;
cin>>x>>y;
A = findFather(x); B = findFather(y);
if(o==2 && A==B && A != 0) cout<<"DA\n";
else if(o==2) cout<<"NU\n";
if(o==2) continue;
if(A == B) continue;
if(grade[A] < grade[B]){
fatherOf[A] = B;
grade[B] += grade[A];
} else{
fatherOf[B] = A;
grade[A] += grade[B];
}
}
return 0;
}