Cod sursa(job #3159724)

Utilizator AlexTrTrambitas Alexandru-Luca AlexTr Data 21 octombrie 2023 21:03:45
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;

int n, m, x, y, i, cod, tata[100001];
int sef(int x)
{
    if (tata[x]==x)
        return x;
    else
        return tata[x] = sef(tata[x]);
}
void unire (int x, int y)
{
    int sefx = sef(x);
    int sefy = sef(y);
    tata[sefy] = sefx;
}

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

    f>> n >> m;
    for (i=1; i<=n; ++i)
        tata[i] = i;
    for (i=1; i<=m; ++i)
    {
        f >> cod >> x >> y;
        if (cod == 1)
            unire(x, y);
        else
        {
            if (tata[x]==tata[y])
                g << "DA\n";
            else
                g << "NU\n";
        }
    }

    f.close();
    g.close();
    return 0;
}