Cod sursa(job #884957)

Utilizator flslatina95Marin Florin flslatina95 Data 21 februarie 2013 15:18:01
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
long n;
long h[100000];
long t[100000];
long u;
inline int drum (int nod)
{
while(t[nod])
    nod=t[nod];
 return nod;
}
inline void paduri(int c, int x,int y)
{
    if(c==1)
    {
        if(drum(x)!=drum(y))
        {
            if(h[x]==h[y])
            {
            t[x]=y;
            h[x]++;
            }
            else
                if(h[x]<h[y])
                    t[x]=y;
                else
                    t[y]=x;
        }

    }
    else
        if(drum(x)==drum(y))
            fout<<"DA"<<'\n';
        else
            fout<<"NU"<<'\n';
}
void cit()
{
    fin>>n>>u;

}
int main()
{
    int i;
    cit();
    int c,x,y;
//  fout<<n<<" "<<u<<'\n';
    for(i=1;i<=u;i++)
    {
        fin>>c>>x>>y;
        paduri(c,x,y);
//fout<<c<<" "<<x<<" "<<y<<'\n';
    }
 //   fout<<'\n';
fout<<'\n';
for(i=1;i<=n;i++)
    fout<<i<<" "<<t[i]<<" ";

   return 0;
}