Cod sursa(job #2515264)

Utilizator blotucosmincosmin blotucosmin Data 28 decembrie 2019 11:31:02
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
using namespace std;
int tata[100001], n, m, t, x, y, i, px, py;
int findroot(int x)
{
    while(tata[x] != x)
        x = tata[x];
    return tata[x];
}
int main()
{
    ifstream f("disjoint.in");
    ofstream g("disjoint.out");
    f >> n >> m;
    for(i = 1; i <= n; ++ i)
        tata[i] = i;
    for(i = 1; i <= m; ++ i)
    {
        f >> t >> x >> y;
        px = findroot(x);
        py = findroot(y);
        if(t == 1)
            tata[px] = py;
        else
        {
            if(px == py) g << "DA" << "\n";
                    else g << "NU" << "\n";
        }
    }
    return 0;
}