Cod sursa(job #1882178)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 16 februarie 2017 23:42:54
Problema Obiective Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.96 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], wcl[DIM], rnk[DIM], tsr[DIM], anc[EXP][DIM];
vector<int> gph[DIM], edg[DIM], stk, ieg[DIM], teg[DIM]; bitset<DIM> mrk;

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

void dfs2( int x, int &k ) {
    mrk[x] = 1;
    
    for( int y : edg[x] ) {
        if( mrk[y] == 0 )
            dfs2( y, k );
    }
    
    tsr[++ k] = x;
    return;
}

void dfs3( int x ) {
    
    for( int i = 1; i < EXP; i ++ )
        anc[i][x] = anc[i - 1][anc[i - 1][x]];
    
    for( int y : teg[x] )
        dfs3( 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 dt = 0, nr = 0;
    for( int i = 1; i <= n; i ++ ) {
        if( lev[i] == 0 )
            dfs1( i, dt, nr );
    }
    
    for( int i = 1; i <= n; i ++ ) {
        for( int x : gph[i] ) {
            if( wcl[i] != wcl[x] ) {
                edg[wcl[i]].push_back( wcl[x] );
                ieg[wcl[x]].push_back( wcl[i] );
                rnk[wcl[x]] ++;
            }
        }
    }
    
    mrk.reset(); n = nr;
    for( int i = 1, k = 0; i <= n; i ++ ) {
        if( rnk[i] == 0 )
            dfs2( i, k );
    }
    
    reverse( tsr + 1, tsr + n + 1 );
    fill( lev + 1, lev + n + 1, 0 );
    
    for( int i = 2; i <= n; i ++ ) {
        lev[tsr[i]] = INF;
        
        for( int x : ieg[tsr[i]] ) {
            if( lev[tsr[i]] > lev[x] + 1 ) {
                lev[tsr[i]] = lev[x] + 1;
                anc[0][tsr[i]] = x;
            }
        }
        
        teg[anc[0][tsr[i]]].push_back( tsr[i] );
    }
    
    dfs3( tsr[1] );
    
    int t;
    in >> t;
    
    for( int i = 1; i <= t; i ++ ) {
        int x, y;
        in >> x >> y;
        
        x = wcl[x], y = wcl[y];
        out << abs( lev[x] - lev[lca( x, y )] ) << "\n";
    }
    
    return 0;
}