Cod sursa(job #3352606)

Utilizator dubitDarius Dubit dubit Data 29 aprilie 2026 13:38:18
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
/*
 * author [dubit]
*/
#include <bits/stdc++.h>

using namespace std;

int root[100005];

int findroot(int x)
{
    if(root[x]==x)
        return x;
    return root[x]=findroot(root[x]);
}

void dsu(int a,int b)
{
    int ra=findroot(a),rb=findroot(b);
    if(ra!=rb)
        root[ra]=rb;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);

    int n,m,cod,x,y;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        root[i]=i;

    while(m--)
    {
        cin>>cod>>x>>y;
        if(cod==1)
            dsu(x,y);
        else
            cout<<((findroot(x)==findroot(y)) ? "DA\n" : "NU\n");
    }
    return 0;
}