Cod sursa(job #2942410)

Utilizator BVLUBogdan Ivan BVLU Data 19 noiembrie 2022 17:35:41
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 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;
        if( op == 1 )
        {
            int tx = gasesteTata( x ), ty = gasesteTata( y );
            t[ty] = tx;
        }
        else
            g << ( ( gasesteTata( x ) == gasesteTata( y ) ) ? "DA\n" : "NU\n" );
    }
    f.close();
    g.close();
    return 0;
}