Cod sursa(job #3367963)

Utilizator AndreibatmanAndrei Croitoriu Andreibatman Data 12 octombrie 2026 15:45:48
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, t[100005], act, x, y;

int fnd(int x)
{
    int y=x,aux;
    while(y!=t[y])
        y=t[y];
    while(x!=t[x]){
        aux=t[y];
        t[x]=y;
        x=aux;
    }
    return x;
}

void unite(int x, int y)
{
    int tx=fnd(x);
    int ty=fnd(y);
    t[ty]=tx;
}

int main()
{
    fin>>n>>m;
    for(int i=1; i<=n; ++i)
        t[i]=i;
    for(int i=1; i<=m; ++i){
        fin>>act>>x>>y;
        if(act==1){
            unite(x, y);
        }
        else{
            if(fnd(x)==fnd(y))
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }
    }
    return 0;
}