Cod sursa(job #2623928)

Utilizator Sho10Andrei Alexandru Sho10 Data 4 iunie 2020 10:25:25
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define modul 998244353
#define PI 3.14159265359
#define CODE_START  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m,sz[100005],parent[100005];
void create(ll x){
parent[x]=x;
sz[x]=1;
}
ll caut(ll x){
if(x==parent[x]){
    return x;
}else return parent[x]=caut(parent[x]);
}
void unire(ll x,ll y){
x=caut(x);
y=caut(y);
if(x!=y){
    if(sz[x]<sz[y]){
        swap(x,y);
    }
    parent[y]=x;
    sz[x]+=sz[y];
    }
}
int32_t main(){
CODE_START;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
cin>>n>>m;
for(ll i=1;i<=n;i++)
{
    create(i);
}
while(m--){
    ll t,a,b;
    cin>>t>>a>>b;
    if(t==1){
        unire(a,b);
    }else if(caut(a)==caut(b)){
    cout<<"DA"<<endl;
    }else cout<<"NU"<<endl;
}
}