Cod sursa(job #2942379)

Utilizator BVLUBogdan Ivan BVLU Data 19 noiembrie 2022 16:57:01
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int n, m;
vector < int > v, cnt;

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