Cod sursa(job #2693986)

Utilizator Harsa_AndreiHarsa Andrei Harsa_Andrei Data 7 ianuarie 2021 19:15:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int tata[100005], n, m;

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

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

    while (tata[rad] != 0)
        rad = tata[rad];

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

    return rad;
}

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

void solve ()
{
    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()
{
    solve();
    return 0;
}