Cod sursa(job #2865970)

Utilizator rebelesRebeles Bogdan rebeles Data 9 martie 2022 11:50:50
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
using namespace std;
struct muchii{int i,j;}M[100001];
int n,m,t[100001],op[100001];
void citire()
{
    int i,j,operatie,nr=0;
    ifstream f("disjoint.in");
    f>>n>>m;
    for(nr=1;nr<=m;nr++)
    {
        f>>operatie>>i>>j;
        M[nr].i=i;
        M[nr].j=j;
        op[nr]=operatie;
    }
    f.close();
}

void afisare()
{
    for(int i=1;i<=m;i++)
        cout<<M[i].i<<' ';
    cout<<'\n';
    for(int i=1;i<=m;i++)
        cout<<M[i].j<<' ';
    cout<<'\n';
    for(int i=1;i<=m;i++)
        cout<<op[i]<<' ';
    cout<<'\n';
}

int rad(int a)
{
    if(a==t[a]) return a;
    else return rad(t[a]);
}

void unire(int a, int b)
{
    if(t[a]>t[b]) t[a]=t[b];
    else t[b]=t[a];
}

int main()
{
    citire();
    afisare();

    for(int i=1;i<=m;i++) t[i]=i;

    ofstream g("disjoint.out");
    int x,y;
    for(int k=1;k<=m;k++)
    {
        x=rad(M[k].i);
        y=rad(M[k].j);
        if(op[k]==1)
        {
            if(x!=y)
                unire(x,y);
        }
        else
        {
            if(x==y) g<<"DA\n";
                else g<<"NU\n";
        }

    }
    g.close();

    return 0;
}