Cod sursa(job #2427330)

Utilizator teoschipor00Teodora Schipor teoschipor00 Data 31 mai 2019 16:27:40
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <iostream>
#include <fstream>
#include <vector>
#define Nmax 100009
using namespace std;

///-----FISIERE-----
ifstream in("disjoint.in");
ofstream out("disjoint.out");

///-----TIPURI DE DATE-----
int tata[Nmax], grad[Nmax];


int FindFather(int nod)
{
    if(tata[nod] == nod)
        return nod;
    tata[nod] = FindFather(tata[nod]);
    return tata[nod];
}
int main()
{
    ///-----CITIRE DATE DIN FISIER-----
    int N, M;
    in >> N >> M;
    for(int i = 1; i <= N; i++)
    {
        tata[i] = i;
        grad[i] = 1;
    }
    int cod, x, y;
    for(int i = 0; i < M; i++)
    {
        in >> cod >> x >> y;
        int fx = FindFather(x);
        int fy = FindFather(y);
        if(cod == 1)
        {
            if(grad[fx] < grad[fy])
            {
                tata[fx] = fy;
                grad[fy] += grad[fx];
            }
            else
            {
                tata[fy] = fx;
                grad[fx] += grad[fy];
            }
        }
        else if(cod == 2)
        {
            if(fx == fy)
                out << "DA" << endl;
            else
                out << "NU" << endl;
        }
    }
    return 0;
}