Cod sursa(job #3328813)

Utilizator apoputoaievladVlad Cristian Apoputoaie apoputoaievlad Data 10 decembrie 2025 15:47:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int NMAX = 100005;
int n, m;
int t[NMAX];
int sz[NMAX];


int root(int nod)
{
    /**int y,x;
    y = nod;
    while(t[nod] != nod)
    {
        nod = t[nod];
    }
    while(y!=nod)
    {
        x=t[y];
        t[y]=nod;
        y=x;
    }
    return nod;*/
    if(t[nod]==nod) return nod;
    t[nod]= root(t[nod]);
    return t[nod];
}

void unite(int x, int y)
{
    x = root(x);
    y = root(y);
    if(x!=y)
    {
        if(sz[x] > sz[y])
            swap (x,y);
        t[x]=y;
        sz[y]+=sz[x];
    }
}

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