Cod sursa(job #2556471)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 24 februarie 2020 22:01:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int tati[100005], n, m;

int getRoot(int x)
{
    if(tati[x] == 0)
        return x;
    int tata = getRoot(tati[x]);
    tati[x] = tata;
    return tata;
}

void mergeTogether(int x, int y)
{
    int xRoot = getRoot(x);
    int yRoot = getRoot(y);
    tati[xRoot] = yRoot;
}

void checkTogether(int x, int y)
{
    int xRoot = getRoot(x);
    int yRoot = getRoot(y);
    if(xRoot == yRoot)
        g<<"DA\n";
    else
        g<<"NU\n";
}

void solve()
{
    f>>n>>m;
    int command, x, y;
    for(int i = 0; i<m; ++i)
    {
        f>>command>>x>>y;
        if(command == 1)
            mergeTogether(x, y);
        else
            checkTogether(x, y);
    }
}

int main()
{
    solve();
    return 0;
}