Cod sursa(job #3232691)

Utilizator PitigoiOlteanEmanuelPitigoi Oltean Emanuel PitigoiOlteanEmanuel Data 1 iunie 2024 07:59:41
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>


using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");

int n;
short int parent[100001],marimi[100001];
int cnt,Max=-1;


int find(int x)
{
    if(parent[x]==x)
    {
        return x;
    }
    return parent[x]=find(parent[x]);
}

int unite(int a,int b)
{
    a=find(a);
    b=find(b);

   if(marimi[a]>marimi[b])
   {
     swap(a,b);
   }
   if(a!=b)
   {
       marimi[b]+=marimi[a];

       parent[a]=b;

   }
   return 0;

}

int main()
{
    int n,q;
    cin>>n>>q;
    cnt=n;
    for(int i=1;i<=n;i++)
    {
        parent[i]=i;
        marimi[i]=1;
    }
    for(int i=1;i<=q;i++)
    {
        int a,b,x;
        cin>>x>>a>>b;
        if(x==1)
        {
             unite(a,b);
        }
        else
        {
            if(find(a)==find(b))
            {
                cout<<"DA"<<'\n';
            }
            else
            {
                cout<<"NU"<<'\n';
            }
        }

    }





}