Cod sursa(job #2575873)

Utilizator CozminelDanielLupu Cosmin-Daniel CozminelDaniel Data 6 martie 2020 15:57:43
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>

using namespace std;

int t[100005], n, m;

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

int Find(int x)
{
    int rad, y;
    rad = x;
    while(t[rad])
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

int main()
{
    ifstream fin("disjoint.in");
    ofstream fout("disjoint.out");
    int c, x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> c >> x >> y;
        x = Find(x);
        y = Find(y);
        if(c == 1)
            Union(x, y);
        else
            if(x == y)
                fout << "DA\n";
            else
                fout << "NU\n";
    }
    fin.close();
    fout.close();
    return 0;
}