Cod sursa(job #1807126)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 16 noiembrie 2016 01:10:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int maxn = 100005;
int F[maxn];

int rad(int nod)
{
    if(F[nod] < 0)
        return nod;
    return rad(F[nod]);
}

void add(int x, int y)
{
    if(x == y)
        return;
    F[y] += F[x];
    F[x] = y;
}

int main()
{
    int n, m;
    in >> n >> m;
    fill(F + 0, F + maxn, -1);
    for(int i = 1; i <= m; i++)
    {
        int op, x, y;
        in >> op >> x >> y;
        if(op == 1)
        {
            int px = rad(x);
            int py = rad(y);
            int dim1 = -F[px];
            int dim2 = -F[py];
            if(dim1 < dim2)
                add(px, py);
            else
                add(py, px);
        }
        else
        {
            if(rad(x) == rad(y))
                out << "DA";
            else
                out << "NU";
            out << "\n";
        }
    }
    return 0;
}