Cod sursa(job #1292514)

Utilizator thinkphpAdrian Statescu thinkphp Data 14 decembrie 2014 13:57:06
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.34 kb
#include <fstream>
#include <iostream>

using namespace std;

typedef unsigned int  uint;
typedef unsigned long ulong;

class Forest {

      public: 
      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;
             } 
      };

      ~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 connected(ulong x, ulong y) {

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

      private:
      ulong* parent;
      ulong* height;
      ulong numElems;   

      ulong find(ulong node) 
      {

           if(parent[ node ] != node) {

              find( parent[ node ] );

           } else return node; 
      };


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

                   node = parent[ node ];  
            }

            return node;
      };
};


int main() {

    const char *FIN = "disjoint.in";
    const char *FOUT = "disjoint.out";

    ifstream fin(FIN);
    ofstream fout(FOUT);


    if(!fin || !fout) {

        cout<<"Error opening files";
        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.connected(x,y)) fout<<"DA"<<"\n"; else fout<<"NU"<<"\n";
                   break;
           } 
     }

    /**
     *  Solve the problem...
     */

    return(0);
};