Cod sursa(job #2853990)

Utilizator Maniu_DianaManiu Maria Diana Maniu_Diana Data 20 februarie 2022 19:57:58
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, x, y, p;
int parinte[100005];

vector < int > L[100005];

int sef(int a)
{
    while(parinte[a] != a)
        a = parinte[a];
    return a;
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i ++)
        parinte[i] = i;

    while(m--)
    {
        fin >> p >> x >> y;
        if(p == 1)
        {
            L[x].push_back(y);
            L[y].push_back(x);
            parinte[y] = x;
        }
        else if(p == 2)
        {
            if(sef(x) == sef(y))
                fout << "DA\n";
            else fout << "NU\n";
        }
    }

    return 0;
}