Cod sursa(job #1885543)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 20 februarie 2017 00:15:44
Problema Obiective Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.06 kb
#include <bits/stdc++.h>
using namespace std;

fstream in ( "obiective.in" , ios::in  );
fstream out( "obiective.out", ios::out );

const int EXP = 15;
const int DIM = 32001;
const int INF = 0x3f3f3f3f;

int low[DIM], lev[DIM], anc[EXP][DIM], fth[EXP][DIM], col[DIM], rnk[DIM];
vector<int> gph[DIM], stk, edg[DIM]; bitset<DIM> mrk;

void dfs1( int x, int &nr, int &dt ) {
    low[x] = lev[x] = ++ dt;
    mrk[x] = 1; stk.push_back( x );
    
    for( int y : gph[x] ) {
        if( lev[y] == 0 )
            dfs1( y, nr, dt );
        if( mrk[y] == 1 )
            low[x] = min( low[x], low[y] );
    }
    
    if( low[x] == lev[x] ) {
        nr ++;
        
        int val;
        do {
            val = stk.back(); col[val] = nr;
            mrk[val] = 0; stk.pop_back();
        } while( val != x );
    }
    
    return;
}

void dfs2( int x, int f ) {
    lev[x] = lev[f] + 1; mrk[x] = 1;
    
    for( int y : edg[x] ) {
        if( mrk[y] == 0 ) {
            dfs2( y, x ); anc[0][y] = x;
            fth[0][x] = min( fth[0][x], fth[0][y] );
        }
    }
    
    return;
}

int lca( int x, int y ) {
    if( lev[x] > lev[y] )
        swap( x, y );
    
    for( int i = EXP - 1; i >= 0; i -- ) {
        if( lev[y] - ( 1 << i ) >= lev[x] )
            y = anc[i][y];
    }
    
    for( int i = EXP - 1; i >= 0; i -- ) {
        if( anc[i][x] != anc[i][y] ) {
            x = anc[i][x];
            y = anc[i][y];
        }
    }
    
    return ( ( x == y ) ? x : anc[0][x] );
}

int main( void ) {
    ios::sync_with_stdio( false );
    
    int n, m;
    in >> n >> m;
    
    for( int i = 1; i <= m; i ++ ) {
        int x, y;
        in >> x >> y;
        
        gph[x].push_back( y );
    }
    
    int nr = 0, dt = 0;
    for( int i = 1; i <= n; i ++ ) {
        if( lev[i] == 0 )
            dfs1( i, nr, dt );
    }
    
    iota( fth[0] + 1, fth[0] + nr + 1, 1 );
    transform( col + 1, col + n + 1, col + 1,
        [nr]( int x ) { return nr - x + 1; } );
    
    for( int i = 1; i <= n; i ++ ) {
        for( int x : gph[i] ) {
            if( col[i] != col[x] ) {
                edg[col[i]].push_back( col[x] ); rnk[col[x]] ++;
                fth[0][col[x]] = min( fth[0][col[x]], col[i] );
            }
        }
    }
    
    for( int i = 1; i <= nr; i ++ ) {
        sort( edg[i].begin(), edg[i].end() );
        edg[i].resize( unique( edg[i].begin(), edg[i].end() ) - edg[i].begin() );
    }
    
    mrk.reset();
    dfs2( 1, 0 );
    
    for( int k = 1; k < EXP; k ++ ) {
        for( int i = 1; i <= nr; i ++ ) {
            anc[k][i] = anc[k - 1][anc[k - 1][i]];
            fth[k][i] = fth[k - 1][fth[k - 1][i]];
        }
    }
    
    int t;
    in >> t;
    
    for( int i = 1; i <= t; i ++ ) {
        int x, y;
        in >> x >> y;
        
        x = col[x]; y = col[y];
        
        int z = lca( x, y ), ans = 1;
        for( int i = EXP - 1; i >= 0; i -- ) {
            if( lev[fth[i][x]] > lev[z] ) {
                x = fth[i][x];
                ans += ( 1 << i );
            }
        }
        
        out << ans - ( x == z ) << "\n";
    }
    
    return 0;
}