Cod sursa(job #1413680)

Utilizator ArmandNMArmand Nicolicioiu ArmandNM Data 2 aprilie 2015 00:33:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>

const int NMAX = 100005;

using namespace std;

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

int N,M;
int type,x,y;
int TT[NMAX];

void Update(int x, int y)
{
    if (x > y)
        swap(x,y);
    TT[x] = y;
}

int Query(int x)
{
    if (TT[x] == x)
        return x;
    TT[x] = Query(TT[x]);
    return TT[x];
}

int main()
{
    f >> N >> M;

    for (int i = 1; i <= N; ++i)
    {
        TT[i] = i;
    }

    for (int i = 1; i <= M; ++i)
    {
        f >> type >> x >> y;
        if (type == 1)
        {
            Update(Query(x),Query(y));
        }
        if (type == 2)
        {
            if (Query(x) == Query(y))
                g << "DA\n";
            else g << "NU\n";
        }
    }

    f.close();
    g.close();
    return 0;
}