Cod sursa(job #2669564)

Utilizator kerry6205Motiu Radu kerry6205 Data 7 noiembrie 2020 11:30:14
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#define O 100005
using namespace std;

int tata[O];
int N, M;

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

int gasire(int x)
{
    if(tata[x] != x)
        tata[x]  = gasire(tata[x]);
    return tata[x];
}

void unire(int a, int b)
{
    if(a != b)
        if(a < b)
            tata[b] = a;
        else
            tata[a] = b;
}

int main()
{
    int x, y, c;
    in >> N >> M;

    for(int i = 1; i <= N; i++)
        tata[i] = i;

    for(int i = 1; i <= M; i++)
    {
        in >> c >> x >> y;

        if(c == 1)
            unire(x, y);
        else
            if(gasire(x) == gasire(y))
                out << "DA" << '\n';
            else
                out << "NU" << '\n';
    }
}