Cod sursa(job #2088475)

Utilizator cristicretancristi cretan cristicretan Data 15 decembrie 2017 12:14:30
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
/// DISJOINT SET OF TREES
#include <iostream>
#include <fstream>
#include <algorithm>
#include <algorithm>
#define NMax 100001
///#define f cin
///#define g cout
using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n, m, cod, x, y, rootx, rooty;
int root[NMax], rang[NMax];

int findroot(int x)
{
    while(x != root[x])
        x = root[x];
    return x;
}
int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        rang[i] = 1;
        root[i] = i;
    }
    for(int i = 0; i < m; ++i)
    {
        f >> cod >> x >> y;
        if(cod == 1)
        {
            rootx = findroot(x);
            rooty = findroot(y);
            if(rang[rootx] > rang[rooty])
            {
                rang[rootx] += rang[rooty];
                root[rooty] = rootx;
            }
            else
            {
                rang[rooty] += rang[rootx];
                root[rootx] = rooty;
            }
        }
        else
        {
            rootx = findroot(x);
            rooty = findroot(y);
            if(rootx == rooty) g << "DA" << '\n';
            else g << "NU" << '\n';
        }
    }
    return 0;
}