Cod sursa(job #1311292)

Utilizator robertstrecheStreche Robert robertstreche Data 7 ianuarie 2015 22:20:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>

#define lmax 100005

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n,m,tip,x,y;
int tata[lmax],inalt[lmax];

inline int find(int nod)
{
    int rad=nod;
    while (tata[rad]!=rad)
     rad=tata[rad];
    while (tata[nod]!=nod)
     {   tata[nod]=rad;
         nod=tata[nod];
     }
   return rad;
}
inline void uneste(int nod1,int nod2)
{
    if (inalt[nod1]>inalt[nod2])
     tata[nod1]=nod2;
    else
     tata[nod2]=nod1;

   if (inalt[nod1]==inalt[nod2])
    inalt[nod2]++;
}
int main()
{
   f>>n>>m;

   for (int i=1;i<=n;i++)
    tata[i]=i;

   for (int i=1;i<=m;i++)
    {
        f>>tip>>x>>y;

        if (tip==1)
         uneste(find(x),find(y));
        else
          if (find(x)==find(y))
           g<<"DA\n";
          else
           g<<"NU\n";
    }

   f.close();
   g.close();
}