Cod sursa(job #2066380)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 14 noiembrie 2017 22:38:27
Problema Paduri de multimi disjuncte Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 1e5 + 10;

int tata[Nmax], nivel[Nmax];

void add_to_the_lot_1(int x, int y)
{
    if(nivel[x] > nivel[y])
        tata[y] = x;
    else
        tata[x] = y;
    if(nivel[x] == nivel[y])
        nivel[x]++;
}

void same_lot_2(int x, int y)
{
    int tata_x = x;
    int tata_y = y;
    while(tata[tata_x] != tata_x) {
        tata_x = tata[tata_x];
    }

    while(tata[tata_y] != tata_y) {
        tata_y = tata[tata_y];
    }

    if(tata_x == tata_y)
        fout << "DA" << '\n';
    else
        fout << "NU" << '\n';
}

int main()
{
    int n, m;
    fin >> n >> m;

    for(int i = 1; i <= m; ++i) {
        tata[i] = i;
        nivel[i] = 1;
    }

    for(int i = 1; i <= m; i++) {
        int cod, x, y;
        fin >>cod >> x >> y;
        if(cod == 1) {
            add_to_the_lot_1(x, y);
        }

        if(cod == 2) {
            same_lot_2(x, y);
        }
    }
    return 0;
}