Cod sursa(job #2359037)

Utilizator calinfloreaCalin Florea calinflorea Data 28 februarie 2019 16:13:18
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
#define NMax 100006

using namespace std;

ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

int t[NMax], n, m;

int Find (int x)
{
    int rad = x, y;

    while (t[rad])
        rad = t[rad];

    while (x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }

    return rad;
}

void Union (int x, int y)
{
    t[x] = y;
}

void Citire ()
{
    int x, y, opt;

    fin >> n >> m;

    for (int i = 1; i <= m; i++)
    {
        fin >> opt >> x >> y;

        x = Find (x);
        y = Find (y);

        if (opt == 1)
            Union (x, y);
        else
            if (x == y)
                fout << "DA\n";
            else
                fout << "NU\n";
    }
}
int main()
{
    Citire();
    return 0;
}