Cod sursa(job #1886306)

Utilizator LaurIleIle Laurentiu Daniel LaurIle Data 20 februarie 2017 20:12:23
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#define nmax 100000+5
using namespace std;

int n, m, cod;
int tata[nmax];

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

int fnd(int x)
{
    int r=x;
    while(tata[r]) r=tata[r];
    int y=x, aux;
    while(y!=r)
    {
        aux=tata[y]; tata[y]=r; y=aux;
    }
    return r;
}

int main()
{
    f >> n >> m;
    for(int i=0, x, y; i<m; ++i)
    {
        f >> cod >> x >> y;
        if(cod == 1)
            tata[x] = y;
        else
        {
            if(fnd(x) == fnd(y))
                g << "DA" << '\n';
            else
                g << "NU" << '\n';
        }
    }
    f.close();
    g.close();
    return 0;
}