Cod sursa(job #668700)

Utilizator feelshiftFeelshift feelshift Data 25 ianuarie 2012 14:53:36
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
// http://infoarena.ro/problema/disjoint
#include <fstream>
using namespace std;

const int MAXSIZE = 100001;

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

int length,operations;
int v[MAXSIZE];

void join(int node,int father);
bool isJoined(int first,int second);

int main()
{
    in >> length >> operations;

    int type,first,second;
    for(int i=1;i<=operations;i++)
    {
        in >> type >> first >> second;

        if(type == 1)
            join(first,second);
        else
        {
            //for(int i=1;i<=length;i++)
              //  out << v[i] << " ";

            if(isJoined(first,second))
                out << "DA\n";
            else
                out << "NU\n";

            //out << "\n";
        }
    }

    return (0);
}

void join(int node,int father)
{
    if(!v[node])
        v[node] = father;
    else
        join(v[node],father);
}

bool isJoined(int first,int second)
{
    if(v[first])
        return isJoined(v[first],second);
    else
        return (first == second);
}