Cod sursa(job #3337518)

Utilizator alexandra_popaa13Popa Alexandra alexandra_popaa13 Data 28 ianuarie 2026 14:37:31
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#define NMAX 100002

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

int n,m;
int tata[NMAX];

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

void uneste(int x,int y)
{
    x=gaseste(x);
    y=gaseste(y);
    if(x!=y)
        tata[y]=x;
}
int main()
{
    int cod,x,y,i;
    fin>>n>>m;
    //initializare tata
    for(i=1; i<=n; i++)
        tata[i]=i;

    for(i=1; i<=m; i++)
    {
        fin>>cod>>x>>y;
        if(cod==1)
            {
             uneste(x,y);
            }
            else
            {
                if(gaseste(x)==gaseste(y))
                    fout<<"DA"<<'\n';
                    else fout<<"NU"<<'\n';
            }
    }
    return 0;
}