Cod sursa(job #2866791)

Utilizator 21CalaDarius Calaianu 21Cala Data 9 martie 2022 23:22:41
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define NMAX 10000001
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n,m,TT[100005],RG[100005];
int find(int x)
{
    int R,y;

    for(R=x;TT[R]!=R;R=TT[R]);

    /*while(TT[x]!=x)
    {
        y = TT[x];
        TT[x]=R;
        x=y;
    }*/

    return R;
}
void unite(int x, int y)
{
    if(RG[x]>RG[y])
        TT[y]=x;
    else
        TT[x]=y;

    if(RG[x]==RG[y])
        RG[y]++;
}
int main()
{
    fin >> n>> m;
    for(int i=1;i<=n;++i)
    {
        TT[i]=i;
        RG[i]=1;
    }
    for(int i=0;i<m;++i)
    {
        int c,x,y;
        fin >> c >> x >> y;
        if(c==2) {
                if(find(x)==find(y)) fout << "DA" << '\n';
                else fout << "NU" << '\n';
        } else {
            if(find(x)!=find(y))
                unite(find(x),find(y));
        }
    }
    return 0;
}