Cod sursa(job #3377202)

Utilizator TeogaloiuTeodor Galoiu Teogaloiu Data 22 noiembrie 2026 11:02:39
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
const int asizeof=1e5;
int sizeoftree[asizeof];
int dad[asizeof];
int find_dad(int node)
{
    if(dad[node]==node)
        return node;
    return(dad[node]=find_dad(dad[node]));
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        dad[i]=i;
        sizeoftree[i]=1;
    }
    for(int i=1;i<=m;i++)
    {
        int cod,x,y;
        cin>>cod>>x>>y;
        if(cod==2)
        {
            cout<< ((find_dad(x)==find_dad(y))? "DA":"NU")<<'\n';
        }
        else
        {
            int dad1=find_dad(x),dad2=find_dad(y);
            if(sizeoftree[dad1]>=sizeoftree[dad2]){
                dad[dad2]=dad1;
                sizeoftree[dad1]+=sizeoftree[dad2];
            }else
            {
                dad[dad1]=dad2;
                sizeoftree[dad2]+=sizeoftree[dad1];
            }
        }
    }
    return 0;
}