Cod sursa(job #2173905)

Utilizator andreimuthMuth Andrei andreimuth Data 16 martie 2018 09:35:38
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#define nmax 100001
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

int n, m, i, j, c, x, y, tata[nmax], fx, fy;

int f (int x)
{
    if (tata[x] == x)
        return x;
    return f (tata[x]);
}

int main()
{
    fin >> n >> m;
    for (i = 1; i <= n; i++)
        tata[i] = i;

    for (i = 1; i <= m; i++)
    {
        fin >> c >> x >> y;
        fx = f (x);
        fy = f (y);
        if (c == 1)
        {
            tata[fx] = fy;
        }
        else
        {
            if (fx == fy)
                fout << "DA\n";
            else fout << "NU\n";
        }

    }
    fin.close ();
    fout.close ();
    return 0;
}