Cod sursa(job #2575458)

Utilizator niculaandreiNicula Andrei Bogdan niculaandrei Data 6 martie 2020 13:39:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
#define N_MAX 100005

using namespace std;

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

int N, M;
int group[N_MAX];

int Find(int x)
{
    int root = x;
    while (group[root] != 0)
        root = group[root];
    while (group[x] != 0)
    {
        int aux = x;
        x = group[x];
        group[aux] = root;
    }
    return root;
}

void Union(int x, int y)
{
    group[Find(y)] = Find(x);
}

int main()
{
    fin >> N >> M;
    for (int i = 1; i <= M; i++)
    {
        int op, x, y;
        fin >> op >> x >> y;
        if (op == 1)
            Union(x, y);
        else
        {
            if (Find(x) == Find(y))
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }
    return 0;
}