Cod sursa(job #2658791)

Utilizator Florinos123Gaina Florin Florinos123 Data 15 octombrie 2020 08:27:56
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f ("disjoint.in");
ofstream g ("disjoint.out");

int n, m;

int poz[100005];

vector < int > v[100005];

void Unire (int x, int y)
{
    if (poz[x] == poz[y])
        return;

    if (v[poz[x]].size() <= v[poz[y]].size())
    {
        int aux = poz[y];

        for (int i=0; i<v[aux].size(); i++)
        {
            v[poz[x]].push_back(v[aux][i]);
            poz[v[aux][i]] = poz[x];
        }
    }
}

void Querry (int x, int y)
{
    if (poz[x] == poz[y])
        g << "DA" << "\n";
    else g << "NU" << "\n";
}

int main()
{
    f >> n >> m;

    for (int i=1; i<=n; i++)
    {
        v[i].push_back(i);
        poz[i] = i;
    }

    while (m --)
    {
        int type, x, y;
        f >> type >> x >> y;

        if (type == 1) Unire(x, y);
        else Querry(x, y);
    }

    return 0;
}