Cod sursa(job #1993455)

Utilizator Alex18maiAlex Enache Alex18mai Data 23 iunie 2017 02:05:01
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>

using namespace std;

ifstream cin("disjoint.in");
ofstream cout("disjoint.out");

int boss[100005];

int stramos (int nod){
    int superboss = nod ;
	while (superboss != boss [superboss]) {
		superboss = boss [superboss] ;
	}
	while (nod != boss [nod]) {
		int aux = boss [nod] ;
		boss [nod] = superboss ;
		nod = aux ;
	}
	return superboss;
}

void unite (int a, int b){
    boss[b]=boss[a];
}



int main()
{
    int n,m;
    cin>>n>>m;
    for (int i=1; i<=n; i++){
        boss[i]=i;
    }
    for (int i=1; i<=m; i++){
        int x,a,b;
        cin>>x>>a>>b;
        if (x==1){
            unite (a, b);
        }
        if (x==2){
            if (stramos(a) == stramos(b)){
                cout<<"DA"<<'\n';
            }
            else{
                cout<<"NU"<<'\n';
            }
        }
    }
    return 0;
}