Cod sursa(job #2942408)

Utilizator BVLUBogdan Ivan BVLU Data 19 noiembrie 2022 17:32:43
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int n, m;
vector < int > t;

int gasesteTata( int n )
{
    if( t[n] == 0 )
        return n;
    t[n] = gasesteTata( t[n] );
    return t[n];
}

int main()
{
    ifstream f("disjoint.in");
    ofstream g("disjoint.out");
    f >> n >> m;
    t = vector < int > ( n + 1, 0 );
    for( int i = 0; i < m; ++i )
    {
        int op, x, y;
        f >> op >> x >> y;
        int tx = gasesteTata( x ), ty = gasesteTata( y );
        if( op == 1 )
            t[ty] = tx;
        else
            g << ( ( t[x] == t[y] ) ? "NU\n" : "DA\n" );
    }
    f.close();
    g.close();
    return 0;
}