Cod sursa(job #2408862)

Utilizator Mihaibv13Mihai Stoian Mihaibv13 Data 18 aprilie 2019 13:31:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
//map<int,int> fatherOf;
int fatherOf[100002];
int grade[100002];
//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;
    ifstream f("disjoint.in");
    ofstream g("disjoint.out");
    f>>n>>m;
    for(int i=1;i<=n;i++){
        fatherOf[i] = i;
        grade[i] = 1;
    }
    for(int i=1;i<=m;i++){
        f>>o;
        f>>x>>y;
        A = findFather(x); B = findFather(y);
        if(o==2 && A==B && A != 0) g<<"DA\n";
        else if(o==2) g<<"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;
}