Cod sursa(job #1590376)

Utilizator gorni97aaa aaa gorni97 Data 4 februarie 2016 22:55:47
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <fstream>
#define maxn 100005
using namespace std;
int main()



{int n,m,i,j,gr[maxn],c,x,y,h[maxn];
fstream f("disjoint.in",ios::in);
fstream g("disjoint.out",ios::out);
f>>n>>m;

for(i=1;i<=n;i++)
    {gr[i]=0;h[i]=0;}

for(i=1;i<=m;i++)
{f>>c>>x>>y;
if(c==1)
{while(gr[x]!=0)
x=gr[x];
while(gr[y]!=0)
y=gr[y];
if(h[x]<h[y])
{gr[x]=y;
h[y]=h[y]+1;}
else
{gr[y]=x;
h[x]=h[x]+1;
}
}

else

{while(gr[x]!=0)
x=gr[x];
while(gr[y]!=0)
y=gr[y];

if(x==y)
    g<<"DA"<<'\n';
else
    g<<"NU"<<'\n';
}

}
g.close();
}