Cod sursa(job #2711593)

Utilizator sebi_info1Olaru Sebastian sebi_info1 Data 24 februarie 2021 14:32:37
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#define Nmax 100010
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m;
int t[Nmax];
int h[Nmax];
int find(int x)
{   int q=x;
    while(q!=t[q]) q=t[q];
    while(x!=t[x])
    {   int y=t[x];
        t[x]=q;
        x=y;
    }
    return q;
}
void uneste(int x,int y)
{   if(h[x]>h[y]) t[y]=x; else t[x]=y;
    if(h[x]==h[y]) h[y]
}
int main()
{   cin>>n>>m;
    for(int i=1;i<=n;i++) {t[i]=i; h[i]=1;}
    for(int op,x,y;m;--m)
    {   cin>>op>>x>>y;
        if(op==2)
            if(find(x)==find(y)) cout<<"DA\n"; else cout<<"NU\n";
        else
            uneste(find(x),find(y));
    }
    return 0;
}