Cod sursa(job #2947917)

Utilizator alexutzIstrate Cristian Alexandru alexutz Data 26 noiembrie 2022 21:43:31
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>

using namespace std;

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


int n,m;
int inaltime[100001];
int tata[100001];


int reprez(int x)
{
   if(tata[x] == x)
        return x;
   tata[x] = reprez(tata[x]);
   return tata[x];
}


void reuniune(int reprezx,int reprezy)
{
    if(inaltime[reprezx] > inaltime[reprezy])
        tata[reprezy] = reprezx;
    else
    {
        tata[reprezx] = reprezy;
        if(inaltime[reprezx] == inaltime[reprezy])
            inaltime[reprezy]++;
    }


}


void afisare(int reprezx,int reprezy)
{
    if(reprezx == reprezy)
        g << "DA" << endl;
    else
        g << "NU" << endl;

}

int main()
{
    f >> n >> m;
    int i,op,x,y;
    for(i=1;i<=n;i++)
    {
        inaltime[i] = 0;
        tata[i] = i;
    }
    int repx,repy;
    for(i=1;i<=m;i++)
    {
        f >> op >> x >> y;
        repx = reprez(x);
        repy = reprez(y);
        if(op == 1)
        {
            if(repx != repy)
                reuniune(repx,repy);
        }
        else
            afisare(repx,repy);

    }





}