Cod sursa(job #3185989)

Utilizator paull122Paul Ion paull122 Data 20 decembrie 2023 23:05:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define MOD 1000000007

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

int parent[100001];

int root(int x)
{
    if(parent[x]==x)
    {
        return x;
    }


    return parent[x]=root(parent[x]);

}
void unite(int a,int b)
{
    a=root(a),b=root(b);
    parent[b]=a;
}
int main()
{
    int n,m;
    fin >> n >> m;
    for(int i=1;i<=n;i++)
    {
        parent[i]=i;
    }
    while(m--)
    {
        int t,x,y;
        fin >> t >> x >> y;
        if(t==1)
        {
            unite(x,y);
        }
        else
        {
            fout << (root(x)==root(y) ? "DA" : "NU");
            fout << "\n";
        }
    }

}