Cod sursa(job #3203131)

Utilizator suimerchantsui merchant suimerchant Data 13 februarie 2024 09:52:13
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
using namespace std;


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


int n,m;


int tata[100005];


int rad(int x)
{
    while(tata[x]>0)
        x=tata[x];
    return x;
}


void unite(int x,int y)
{
    int rx=rad(x);
    int ry=rad(y);
    if(tata[rx]<tata[ry])
    {
        /// mut pe ry in rx
        tata[rx]+=tata[ry];
        tata[ry]=rx;
    }
    else
    {
        /// mut pe rx in ry
        tata[ry]+=tata[rx];
        tata[rx]=ry;
    }
}


int main()
{
    cout<<"VAMAS"<<"\n";
    cout<<"suveran samanumanunuisaman";
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        tata[i]=-1;
    int c,x,y;
    for(int i=1;i<=m;i++)
    {
        fin>>c>>x>>y;
        if(c==1)
        {
            unite(x,y);
        }
        else
        {
            if(rad(x)==rad(y)) fout<<"DA"<<"\n";
            else fout<<"NU"<<"\n";
        }
    }
    return 0;
}