Cod sursa(job #2572245)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 5 martie 2020 12:16:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda OJI 2020 Marime 1.29 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

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

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int DMAX = 1e5+10;

int tata[DMAX];

int n,m;

int comp(int x);
void unire(int x,int y);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t,i,j;
    int x,y,z;

    fin>>n>>m;
    while(m--){
        fin>>x>>y>>z;
        if(x == 1){
            if(comp(y) != comp(z))
                unire(y,z);
        }
        else{
            (comp(y) == comp(z))?fout<<"DA\n":fout<<"NU\n";
        }
    }

    return 0;
}
int comp(int x){
    int ans=x,aux;
    while(tata[ans])
        ans=tata[ans];
    while(tata[x]){
        aux=x;
        x=tata[x];
        tata[aux]=ans;
    }
    return ans;
}
void unire(int x,int y){
    x=comp(x);
    y=comp(y);
    tata[x]=y;
}