Pagini recente » Cod sursa (job #3235016) | Cod sursa (job #2656210) | Cod sursa (job #513497) | Cod sursa (job #1945402) | Cod sursa (job #2540600)
#include <fstream>
#include <map>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
map<int,int> nodeComp;
map<int,int> compSize;
int maxComp;
int compConex = 33000;
int main(){
int n,m;
cin>>n>>m;
for(int i = 0 ; i < m; i++){
int type, a,b;
cin>>type;
if(type != 3)
cin>>a>>b;
if(type == 1){
if(nodeComp[a] == 0 && nodeComp[b] == 0){
nodeComp[a] = compConex;
nodeComp[b] = compConex;
compConex++;
} else if(nodeComp[a] == 0 && nodeComp[b] != 0){
nodeComp[a] = nodeComp[b];
}else if(nodeComp[a] != 0 && nodeComp[b] == 0){
nodeComp[b] = nodeComp[a];
}else{
int tmp = nodeComp[b];
for(map<int,int>::iterator it= nodeComp.begin(); it!= nodeComp.end(); it++){
if(it->second == tmp){
it->second = nodeComp[a];
}
}
}
} else if(type == 2){
if(nodeComp[a] == nodeComp[b]){
cout<<"DA\n";
} else {
cout<<"NU\n";
}
}
}
return 0;
}