Cod sursa(job #1293589)

Utilizator thinkphpAdrian Statescu thinkphp Data 16 decembrie 2014 06:18:50
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
/**
 *  Disjoint-set Forests Data Structure
 */
#include <fstream>
#include <iostream>
#define FIN "disjoint.in"
#define FOUT "disjoint.out"

using namespace std;

typedef unsigned int  uint;
typedef unsigned long ulong;

class Forest {

      public: 
      //constructor of the class
      Forest(ulong nElements): parent( new ulong[ nElements + 1 ] ), height( new ulong[ nElements + 1 ] ), numElems( nElements )
      {
             for(uint i = 1; i <= numElems; i++) {

                 parent[ i ] = i;

                 height[ i ] = 1;
             } 
      };

      //deconstructor of the class
      ~Forest()
      {
        delete [] parent;    

        delete [] height;
      };  

      void unite(ulong x, ulong y) 
      {

           x = find( x );

           y = find( y );

           if( height[ x ] > height[ y ] ) {

               parent[ y ] = x;

           } else {

               parent[ x ] = y;
           }

           if(height[ x ] == height[ y ]) height[ y ]++;
      };

      bool same(ulong x, ulong y) {

           return find(x) == find(y);
      }; 

      private:

      ulong* parent;
      ulong* height;
      ulong numElems;   

      ulong find(ulong node) 
      {
 
            while( node != parent[ node ]) {

                   node = parent[ node ];  
            }

            return node;
      };
};

//main function
int main() {

    ifstream fin(FIN);

    ofstream fout(FOUT);

    if( !fin || !fout ) {

        cout<<"Error opening files"<<endl;
        return -1;
    }        

    ulong numElements,

          numOps,

          type,

          x,

          y; 

    /**
     *  Read the data in...
     */

     fin>>numElements>>numOps;

    /**
     *  Solve the problem...
     */
 
     Forest forest( numElements );       
  
     for(; numOps; numOps--) {

           fin>>type>>x>>y;

           switch( type ) {

                   case 1:
                   forest.unite(x, y);                    
                   break;
          
                   case 2:
                   if(forest.same(x, y) ) fout<<"DA"<<"\n"; 

                                     else fout<<"NU"<<"\n";
                   break;
           } 
     }

    return(0);
};