Cod sursa(job #2540600)

Utilizator skeniaTirla Ovidiu skenia Data 7 februarie 2020 12:46:52
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#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;
}