Cod sursa(job #1754029)

Utilizator GinguIonutGinguIonut GinguIonut Data 7 septembrie 2016 14:36:15
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <vector>

#define nMax 100001
using namespace std;

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

int n, m;
int Root[nMax];

int getRoot(int node)
{
    return (Root[node]<0 ? node : Root[node]=getRoot(Root[node]));
}

void read()
{
    fin>>n>>m;

    for(int i=1; i<=n; i++)
        Root[i]=-1;
}

void solve()
{
    int op, x, y;

    for(int i=1; i<=m; i++)
    {
        fin>>op>>x>>y;

        if(op==1)
        {
            int val1=getRoot(x), val2=getRoot(y);

            switch(Root[val1]-Root[val2]<0)
            {
                case 1:Root[val1]+=Root[val2]; Root[val2]=val1; break;
                case 0:Root[val2]+=Root[val1]; Root[val1]=val2; break;
            }

            continue;
        }

        int val1=getRoot(x), val2=getRoot(y);

        if(val1!=val2)
            fout<<"NU"<<'\n';
        else
            fout<<"DA"<<'\n';

    }
}
int main()
{
    read();
    solve();
}