Cod sursa(job #1467009)

Utilizator VictoriaNevTascau Victoria VictoriaNev Data 2 august 2015 14:49:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>

using namespace std;
const int NMAX=100001;
int t[NMAX], n, k, i, x, y, tip;
int find(int x)
{
    int pos=x, pos1=x;
    while(t[pos])
        pos=t[pos];
    while(t[pos1])
    {
        int cpos1=pos1;
        pos1=t[pos1];
        t[cpos1]=pos;
    }
    return pos;
}
void unite(int x, int y)
{
    t[find(x)]=find(y);
}
int main()
{
    ifstream in("disjoint.in");
    ofstream out("disjoint.out");
    in>>n>>k;
    for(i=1; i<=k; ++i)
    {
        in>>tip>>x>>y;
        if(tip==1)
            unite(x,y);
        else
        {
            if(find(x)==find(y))
                out<<"DA\n";
            else out<<"NU\n";
        }
    }
    return 0;
}