Cod sursa(job #3317673)

Utilizator stroescu_andreiStroescu Andrei stroescu_andrei Data 24 octombrie 2025 20:43:13
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include<fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int sef[100001];
int boss(int x)
{
    if(sef[x]==x)
        return x;
    else
        return sef[x]=boss(sef[x]);
}
void unire(int x,int y)
{
    int sef_x=boss(x);
    int sef_y=boss(y);
    sef[sef_y]=sef_x;
}
int main()
{
    int i,M,N,cod,x,y;
    f>>N>>M;
    for(i=1;i<=N;i++)
        sef[i]=i;
    for(i=1;i<=M;i++)
    {
        f>>cod>>x>>y;
        if(cod==1)
            unire(x,y);
        else
        {
            if(boss(x)==boss(y))
                g<<"DA"<<endl;
            else
                g<<"NU"<<endl;
        }
    }
}