Cod sursa(job #1885546)

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

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;
    
    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }
    
public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }
    
    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
    
    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
} in( "obiective.in" );

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;
    
    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }
    
    
public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }
    
    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }
    
    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }
    
    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
} out( "obiective.out" );

const int EXP = 15;
const int DIM = 32001;

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