Cod sursa(job #3284968)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 12 martie 2025 13:26:30
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#define nmax 100001
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,t[nmax],task,x,y;
int get_root(int x){
    if(t[x]>0)
        return get_root(t[x]);
    return x;
}
void join(int x,int y){
    x=get_root(x),y=get_root(y);
    if(x==y)
        return;
    if(t[x]>t[y])
        swap(x,y);
    t[x]+=t[y];
    t[y]=x;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        t[i]=-1;
    while(m--){
        cin>>task>>x>>y;
        if(task==1)
            join(x,y);
        else if(get_root(x)==get_root(y))
            cout<<"DA\n";
        else
            cout<<"NU\n";
    }
    return 0;
}