Cod sursa(job #2215837)

Utilizator HumikoPostu Alexandru Humiko Data 23 iunie 2018 20:07:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
#define lim 100005

using namespace std;

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

int n, m, operation_Type, first_Node, second_Node;
int father[lim];

int parent( int node)
{
    if ( node == father[node] )
        return node;
    father[node] = parent(father[node]);
    return father[node];
}

void unite (int node_X, int node_Y)
{
    father[parent(node_X)] = parent(node_Y);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    fin>>n>>m;
    for ( int i = 1; i <= n; ++i )
        father[i] = i;
    for ( int i = 1; i <= m; ++i )
    {
        fin>>operation_Type>>first_Node>>second_Node;
        if ( operation_Type == 1 )
            unite(first_Node, second_Node);
        else
            if ( parent(first_Node) == parent(second_Node) )
                fout<<"DA"<<'\n';
            else
                fout<<"NU"<<'\n';
    }
}