Cod sursa(job #2850168)

Utilizator LionMan101Achim-Panescu Silvian LionMan101 Data 16 februarie 2022 12:34:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.45 kb
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int INF=1e9+7;
const ll INFF=1e18+7;
#define nl '\n'
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char* x) { cout << '\"' << x << '\"'; }
void __print(const string& x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cout << '{'; for (auto& i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; }
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

vector<int> a(100001,-1);

int get_root(int x){
    int root=x;
    while(a[root]>0){
        root=a[root];
    }
    while(x!=root){
        int aux=a[x];
        a[x]=root;
        x=aux;
    }
    return root;
}

bool same(int x, int y){
    int root1=get_root(x);
    int root2=get_root(y);
    return root1==root2;
}

void join(int x, int y){
    int root1=get_root(x);
    int root2=get_root(y);
    if(a[root1]<=a[root2]){
        a[root1]+=a[root2];
        a[root2]=root1;
    }
    else{
        a[root2]+=a[root1];
        a[root1]=root2;
    }
}

void solve()
{
    int n,m;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        int c,x,y;
        cin>>c>>x>>y;
        if(c==1) join(x,y);
        else cout<<(same(x,y) ? "DA\n":"NU\n");
    }
}

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    // int t;
    // cin >> t;
    for(int tt=1; tt<=t; tt++){
        // cout<<"#Case "<<t<<nl;
        solve();
    }
    return 0;
}