Cod sursa(job #2491770)

Utilizator traiandobrinDobrin Traian traiandobrin Data 13 noiembrie 2019 09:12:19
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

using namespace std;
//chestii
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100005];
int h[100005];
int findset(int x)
{
    while(x!=t[x])
        x=t[x];
    return x;
}
void unite(int x,int y)
{
    if(h[x]==h[y])
        h[x]++,t[y]=x;
    else
        if(h[x]>h[y]) t[y]=x;
    else
        t[x]=y;
}
int main()
{
    int n,m,x,y;
    in>>n>>m;
    for(int i=1;i<=n;++i)
    {
        h[i]=1;
        t[i]=i;
    }
    for(int i=1;i<=m;++i)
    {int tip;
        in>>tip>>x>>y;
        int tx=findset(x);
        int ty=findset(y);
        if(tip==1){
        if(ty!=tx)
            unite(x,y);}
            else{
                if(tx==ty) out<<"DA";
            else
                out<<"NU";
                out<<'\n';
 }   }
    return 0;
}