Cod sursa(job #2306927)

Utilizator EdgeLordXDOvidiuPita EdgeLordXD Data 23 decembrie 2018 11:39:32
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
#define N 100001
#define ll long long
#define f first
#define s second
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int a[N], g[N];
int gr(int i){
    if(a[i]!=i)
        a[i]=gr(a[i]);
    return a[i];
}
int main(){
    int n,m,i,x,y,t,g1,g2;
    in>>n>>m;
    for(i=1; i<=n; ++i)
        a[i]=i;
    while(m--){
        in>>t>>x>>y;
        if(t==1){
            g1=gr(x), g2=gr(y);
            if(g[g1]>g[g2])
                a[y]=x;
            else if(g[g1]<g[g2])
                a[x]=y;
            else
                a[y]=x, ++g[x];
        }
        else if(a[gr(x)]==a[gr(y)]) out<<"DA\n";
        else out<<"NU\n";
    }
    return 0;
}