Cod sursa(job #3278830)

Utilizator Tudor28Ceclan Tudor Tudor28 Data 20 februarie 2025 20:50:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <iostream>
#include <numeric>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int dad[100005];
int find(int x)
{

    if (dad[x] == x)
        return x;

    return dad[x] = find(dad[x]);
}
int main()
{
    int n, m;
    fin >> n >> m;
    iota(dad + 1, dad + n + 1, 1);
    int c, x, y;
    while (m--)
    {

        fin >> c >> x >> y;
        int nx = find(x);
        int ny = find(y);
        if (c == 1)
        {
            if (ny > nx)
            {
                dad[ny] = nx;
            }
            else
            {
                dad[nx] = ny;
            }
        }
        else
        {
            if (nx == ny)
            {
                fout << "DA\n";
            }
            else
            {
                fout << "NU\n";
            }
        }
    }
    return 0;
}