Cod sursa(job #1340238)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 11 februarie 2015 18:15:03
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
using namespace std;
#include <fstream>
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define Nmax 100001

int tata[Nmax];

int find(int) ;

int main()
{
    int n, m, x, y, tip;
    bool rand;
    
    fin >> n >> m;
        
    for(; m; --m)
    {
        fin >> tip >> x >> y;
        if(tip == 1)
        {
            rand = (x + y) & 1;
            if(rand)
            {
                x = find(x); //x - radacina arborelui cu nodul x
                tata[x] = y;
            }
            else
            {
                y = find(y); //nod1 - radacina arborelui cu nodul y
                tata[y] = x;
            }
        }
        else
        {
            if(find(x) == find(y)) fout << "DA\n";
            else fout << "NU\n";
        }
    }
    return 0;
}


int find(int x)
{
    if(tata[x] == 0) return x;
    
    tata[x] = find(tata[x]);    
    return tata[x];
}