Cod sursa(job #2470993)

Utilizator Catalin2002Catalin Craciun Catalin2002 Data 9 octombrie 2019 23:02:07
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

struct nod{

int val;
nod *urm;
}*p;

struct componenta_vector{

int poz;

nod *prim;
nod *ultim;

}v[100005];

void operatia_nr_1(int x,int y)
{

   int unde_se_afla_x = v[x].poz;

   while(v[unde_se_afla_x].poz!=unde_se_afla_x)
        unde_se_afla_x = v[unde_se_afla_x].poz;

   int unde_se_afla_y = v[y].poz;

   while(v[unde_se_afla_y].poz!=unde_se_afla_y)
        unde_se_afla_y = v[unde_se_afla_y].poz;


   v[unde_se_afla_y].ultim ->urm = v[unde_se_afla_x].prim ;
   v[unde_se_afla_y].ultim = v[unde_se_afla_x].ultim ;

   v[unde_se_afla_x].poz = v[y].poz ;
   v[x].poz =v[y].poz ;

}

bool operatia_nr_2(int x, int y)
{
    int unde_se_afla_x = v[x].poz;

    while(v[unde_se_afla_x].poz!=unde_se_afla_x)
        unde_se_afla_x = v[unde_se_afla_x].poz;

    int unde_se_afla_y = v[y].poz;

    while(v[unde_se_afla_y].poz!=unde_se_afla_y)
        unde_se_afla_y = v[unde_se_afla_y].poz;


    if(unde_se_afla_x == unde_se_afla_y)
        return true;


    return false;

}

int main()
{
    int n,m,i,c,x,y;

    fin>>n>>m;

    for(i=1;i<=n;i++)
    {
        v[i].poz = i;

        v[i].prim= new nod;
        v[i].prim->val = i;
        v[i].ultim= v[i].prim;

    }

    for(i=1;i<=m;i++)
    {
        fin>>c>>x>>y;

        if(c==1)
            operatia_nr_1(x,y);
        else
        {
            bool sem= operatia_nr_2(x,y);

            if(sem)
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }

    }



    return 0;
}