Cod sursa(job #1795027)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 1 noiembrie 2016 21:54:11
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nMax = 100005;
ifstream in("disjoint.in");
ofstream out("disjoint.out");

int n, m;
int father[nMax];

int GetFather(int x)
{
    if(father[x] != x)
        father[x] = GetFather(father[x]);
    return father[x];
}

void citire()
{
    in >> n >> m;
    for(int i = 1; i <= n; ++i)
        father[i] = i;
}

void rezolvare()
{
    int x, y, t, fx, fy;
    for(int i = 1; i <= m; ++i)
    {
        in >> t >> x >> y;
        fx = GetFather(x);
        fy = GetFather(y);
        if(t == 1)
            father[fx] = fy;
        else
        {
            if(fx == fy) out << "DA\n";
            else out << "NU\n";
        }
    }
}

int main()
{
    citire();
    rezolvare();
    return 0;
}