Cod sursa(job #2041992)

Utilizator Tataru_AdelinTataru Adelin Tataru_Adelin Data 17 octombrie 2017 22:26:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

int get_root(int node,vector<int> &nodeFather)
{
    if(nodeFather[node]==node)
        return node;
    else
    {
        nodeFather[node]=get_root(nodeFather[node],nodeFather);
        return nodeFather[node];
    }
}

int main()
{

    int n,o,type,x,y;
    fin>>n>>o;
    vector<int> nodeFather(n+5);
    for(int i=1;i<=n;i++)
    {
        nodeFather[i]=i;
    }
    for(;o;o--)
    {
        fin>>type>>x>>y;
        x=get_root(x,nodeFather);
        y=get_root(y,nodeFather);
        if(type==1)
        {
            nodeFather[y]=x;
        }
        else
        {
            if(x==y)
            {
                fout<<"DA\n";
            }
            else
            {
                fout<<"NU\n";
            }
        }
    }

    return 0;
}