Cod sursa(job #3185879)

Utilizator paull122Paul Ion paull122 Data 20 decembrie 2023 19:22:28
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 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 height[100001];
int root(int x)
{
    if(parent[x]==x)
    {
        return x;
    }
    else
    {
        return parent[x]=root(parent[x]);
    }
}
int unite(int a,int b)
{
    a=root(a),b=root(b);
    if(height[a] < height[b])
    {
        swap(a,b);
    }
    parent[b]=a;
    height[a]+=height[b];
}
int main()
{
    int n,m;
    fin >> n >> m;
    for(int i=1;i<=n;i++)
    {
        parent[i]=i,height[i]=1;
    }
    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";
        }
    }

}