Cod sursa(job #2525954)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 18 ianuarie 2020 09:29:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 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;

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

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

    int t,i,op,x,y;

    fin>>n>>m;
    for(i=1;i<=m;i++){
        fin>>op>>x>>y;
        if(op & 1){
            if(comp(x) != comp(y))
                unire(x,y);
        }
        else{
            if(comp(x) == comp(y))
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }
    }

    return 0;
}
int comp(int x){
    int root = x;
    while(tata[root])
        root = tata[root];

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