Cod sursa(job #794641)

Utilizator Theorytheo .c Theory Data 6 octombrie 2012 18:35:42
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<fstream>
#define nmax 100007
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int N, R[nmax], T[nmax], nr;

int find(int x)
{
    int i;
    for( i = x; i != T[i] ;i = T[i]) ;
    int j = x;
    do
    {
        int aux = j;
        j = T[j] ;
        T[aux] = i;

    }while(T[j] != j );


    return i;
}
void reunion(int x, int y)
{
    if(R[x] > R[y] )
    {
        T[y] = x;
        R[x]++;
    }
    else{
        T[x] = y;
        R[y]++;

    }

}
void read()
{
    fin >> N>>nr;
    for(int i = 1; i <= N; i++)
    {

        T[i] = i;
        R[i] = 1;
    }
    for(int i = 1; i <= nr; i++)
    {
        int type,x,y;
        fin >>type >>x >>y;
        if(type == 1)
        {
            reunion(find(x),find(y));

        }
        else
        {
            if(find(x) == find(y))
                fout<< "DA";
                 else
                fout<< "NU";
            fout<<'\n';

        }
    }
}
int main()
{
    read();
    fin.close();
    fout.close();
    return 0;

}