Cod sursa(job #1220164)

Utilizator lucian666Vasilut Lucian lucian666 Data 16 august 2014 17:48:55
Problema Lowest Common Ancestor Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.69 kb



#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>

#define NMAX 100009
#define LGMAX 20
#define pb push_back

using namespace std;
ofstream out("lca.out");
ifstream in("lca.in");

int n , m , K , H[NMAX] , L[NMAX] , First[NMAX] , dp[NMAX][LGMAX] ,LG[NMAX] ;

vector <int>G[NMAX];
typedef vector<int>:: iterator IT;

void read();
void dfs( int nod , int level );
void RMQ();
void wrs();

int main()
{

    read();
    dfs(1,1);
    /*
    for( int i=1 ; i<=K ; ++i)
        out << H[i] << " ";

    out << '\n';

    for( int i=1 ; i<=K ; ++i )
        out << L[i] << " ";

        out << '\n';
    */

    RMQ();
    wrs();

    return 0;

}

void read()
{

    in >> n >> m;
    for( int i=2 ; i<=n ; i++)
    {
        int x;
        in >> x;
        G[x].pb(i);
    }


}

void dfs( int nod , int level )
{

    H[ ++K ] = nod;
    L[ K ] = level;
    First[ nod ] = K;

    for( IT i = G[nod].begin() ; i!= G[nod].end(); ++i )
    {

        dfs( *i , level + 1 );
        H[ ++K ] = nod;
        L[ K ] = level;

    }

}

void RMQ()
{
    /* ca sa fie normal tre sa modific :
        for( int i=1 ; i<=K ; ++i )
        dp[i][0] = H[i];

        iar in dinamica ....
        if( i + ( 1 << ( j - 1 )) <=K )
                dp[i][j] = min( dp[i][j] ,  dp[ i + ( 1 << ( j-1 ) ) ][j-1] ) ;

    */
    //rmq de aici imi da poz minimului din vectorul L

    for( int i=2 ; i<=K ; i++)
        LG[i] = LG[ i >> 1 ] + 1;

    for( int i=1 ; i<=K ; i++ )
        dp[i][0] = i;


    int k = 0;
    while( ( 1 << k ) <=K )
        ++k;

    --k;

    for( int j=1 ; j<=k ; j++ )
    {

        for( int i=1 ; i<=K ; i++ )
        {

            dp[i][j] = dp[i][j-1];

            if( i + ( 1 << ( j - 1 )) <=K )
                if( L [ dp[i][j] ] > L [ dp[ i + ( 1 << ( j -1 ) ) ][j-1] ] )
                dp[i][j] =  dp[ i + ( 1 << ( j-1 ) ) ][j-1] ;

        }
    }



}


void wrs()
{

    for( int i , j ; m ; --m )
    {
        in >> i >> j;
       // out << i << " " << j << '\n';

        int x , y;
         int diff, l, sol, sh;
        x = First[i];
        y = First[j];

     //   out << x << " " << y << '\n';
     //  out << '\n';
     //   out << '\n';

        if( x > y )
            swap(x,y);


        diff = y - x + 1;
        l = LG[diff];

        sol = dp[x][l];

        if( L[sol] > L[ dp[ y - ( 1 << l ) + 1 ][l]] )
            sol = dp[ y - ( 1 << l ) + 1 ][l];



       // int k = floor( log ( y - x  ) / log( 2 ));

       // out <<  min ( dp[x][k] , dp[ y - ( 1 << k ) + 1][k]  ) << '\n';
    out << H[sol] << '\n';

    }

}