Cod sursa(job #2936716)

Utilizator antonia2003antonia oancea antonia2003 Data 9 noiembrie 2022 12:17:48
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int>tata,h;
int Find(int u)
{
    if (tata[u]==0)return u;
    tata[u]=Find(tata[u]);
    return tata[u];
}
void Union(int repU, int repV)
{

    if(repU==repV)return;
    if(h[repU]==h[repV])
    {
        tata[repV]=repU;
        //h[repU]++;
        //return;
    }
    else
        if(h[repU]>h[repV])
            {
                tata[repV]=repU;
                //h[repU]++;
               // return;
            }
    else
         {
             tata[repU]=repV;
            // return;
        }

}
int main()
{
    int n,m;
    f>>n>>m;
    tata.resize(n+1,0);
    h.resize(n+1,0);
    int cod,x,y;
    for(int i=0;i<m;i++)
    {
        f>>cod>>x>>y;
        int repU=Find(x);
        int repV=Find(y);
        if(cod==1)
        {
            Union(repU,repV);
        }
        else
        {
            if(repU==repV)
                g<<"DA"<<endl;
            else
                g<<"NU"<<endl;
        }
    }
    return 0;
}