Cod sursa(job #2849096)

Utilizator starduststardust stardust Data 14 februarie 2022 15:32:07
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>
using namespace std;

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

int n, m;
vector<int> r;
vector<int> p;

int find(int x)
{
    while (x != p[x])
        x = p[x];

    return x;
}
void make_union(int x, int y)
{
    x = find(x);
    y = find(y);

    if (r[x] == r[y])
        r[x]++;

    if (r[x] > r[y])
        p[y] = x;
    else
        p[x] = y;
}
int main()
{
    in >> n >> m;
    int x, y, z;

    for (int i = 0; i <= n; i++)
    {
        p.push_back(i);
        r.push_back(0);
    }
    for (int i = 0; i < m; i++)
    {
        in >> x >> y >> z;
        if (x == 1)
            make_union(y, z);
        else
        {
            if (find(y) == find(z))
                out << "DA\n";
            else
                out << "NU\n";
        }
    }
}