Cod sursa(job #2869916)

Utilizator Stefan_BircaBirca Stefan Stefan_Birca Data 11 martie 2022 22:11:56
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, t[100005];

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

int Find(int x)
{
    int y, rad = x;
    while (t[rad] != 0) rad = t[rad];
    while (rad != x)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

int main()
{

    int m, task, x, y;
    fin >> n >> m;
    while (m--)
    {
        fin >> task >> x >> y;
        x = Find(x);
        y = Find(y);
        if (task == 1 && x != y) Union(x, y);
        else if (task == 2)
        {
            if (x == y) fout << "DA\n";
            else fout << "NU\n";
        }
    }
    fin.close();
    fout.close();

    return 0;
}