Cod sursa(job #2514573)

Utilizator dumitrustefaniaDumitru Stefania dumitrustefania Data 26 decembrie 2019 14:04:07
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
#define nmax 100001
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int x,y,xx,yy,cost,cost_total,k,n,m,i,t[nmax],r[nmax],cod;


int findset(int x)
{
    int nod = x;
    while (t[x]!=x)
        x=t[x];
    while (t[nod]!=nod)
    {
        int aux = t[nod];
        t[nod]=x;
        nod = aux;
    }
    return x;
}
void union_set(int x,int y)
{
    if (r[x]>r[y])
        t[y]=x;
    else if (r[y]>r[x])
        t[x]=y;
    else
    {
        r[x]++;
        t[y]=x;
    }
}
int main()
{
    in>>n>>m;
    for (i=1; i<=n; ++i)
    {
        t[i]=i;
        r[i]=1;
    }
    for (i=1; i<=m; ++i)
    {
        in>>cod>>xx>>yy;
        int x = findset(xx);
        int y = findset(yy);
        if(cod==1)
        {
           union_set(x,y);

        }
        else
        {
            if(x==y)
                out<<"DA";
            else
                out<<"NU";
            out<<'\n';
        }

    }


}