Cod sursa(job #3307486)

Utilizator iordacheMatei Iordache iordache Data 21 august 2025 12:13:50
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>
#define pb push_back
//#define int long long
using namespace std;
const int N=1e5+5;
int p[N],sz[N];
int get(int x)
{
    if(p[x]==x) return x;
    return p[x]=get(p[x]);
}
void unite(int x, int y)
{
    x=get(x);y=get(y);
    if(x==y) return;
    if(sz[x]>sz[y]) swap(x,y);
    sz[y]+=sz[x];
    p[x]=y;
}
signed main()
{
    ifstream cin("disjoint.in");ofstream cout("disjoint.out");
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;++i) p[i]=i,sz[i]=1;
    while(q--)
    {
        int op;cin>>op;
        if(op==1)
        {
            int x,y;cin>>x>>y;
            unite(x,y);
        }
        else
        {
            int x,y;cin>>x>>y;
            x=get(x);y=get(y);
            cout<<(x==y ? "DA":"NU")<<'\n';
        }
    }
}