Cod sursa(job #2845444)

Utilizator Theo14Ancuta Theodor Theo14 Data 7 februarie 2022 20:40:01
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include<bits/stdc++.h>
using namespace std;

int v[100005],n,m;

int root(int x)
{
    while(v[x]>0)
        x=v[x];
    return x;
}

void tie(int x, int y)
{
    x=root(x);
    y=root(y);
    if(x==y)
        return;
    if(v[x]<v[y])
    {
        v[x]+=v[y];
        v[y]=x;
    }
    else
    {
        v[y]+=v[x];
        v[x]=y;
    }
}

int main()
{
    int i,ok,x,y;
    cin>>n>>m;
    for(i=1;i<=n;i++)
        v[i]=-1;
    for(i=1;i<=m;i++)
    {
        cin>>ok>>x>>y;
        if(ok==1)
        {
            tie(x,y);
        }
        if(ok==2)
        {
            if(root(x)==root(y))
                cout<<"DA";
            else
                cout<<"NU";
            cout<<'\n';
        }
    }
    return 0;
}