Cod sursa(job #3318197)

Utilizator Lex._.Lex Guiman Lex._. Data 27 octombrie 2025 14:52:55
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
#define NMAX 100001
using namespace std;

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

int t[NMAX], sz[NMAX];

int tata(int nod)
{
    while(t[nod]!=0) nod=t[nod];
    return nod;
}

int main()
{
    int n, m;
    in >> n >> m;
    for(int i=1; i<=n; i++) sz[i]=1;
    while(m--)
    {
        int tip, x, y;
        in >> tip >> x >> y;
        if(tip==1)
        {
            int tata_x=tata(x), tata_y=tata(y);
            if(sz[tata_x]<sz[tata_y])
            {
                t[tata_x]=tata_y;
                sz[tata_y]+=sz[tata_x];
            }
            else
            {
                t[tata_y]=tata_x;
                sz[tata_x]+=sz[tata_y];
            }
        }
        else
        {
            int tata_x=tata(x), tata_y=tata(y);
            if(tata_x==tata_y) out << "DA\n";
            else out << "NU\n";
        }
    }

    return 0;
}