Cod sursa(job #1390369)

Utilizator taigi100Cazacu Robert taigi100 Data 16 martie 2015 23:49:33
Problema Obiective Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.52 kb
/*
    Keep It Simple!
*/

#include <fstream>
#include <vector>
#include <list>
#include <stack>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cstring>

using namespace std;

ifstream fin("obiective.in");
ofstream fout("obiective.out");

#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int kMaxN = 32005;
const int kMaxLb = 20;
const int kInf = 0x3f3f3f3f;

vector<int> G[kMaxN],TG[kMaxN];
stack<int> Kos_st;
set<int> CTC_G[kMaxN];
int rmq[kMaxLb][kMaxN << 1], euler_sz, first[kMaxN];
int anc[kMaxLb][kMaxN];
int ctc[kMaxN], ctc_sz;
int lb[kMaxN << 1];
int N,M;
bool used[kMaxN];

void ReadInput()
{
    fin >> N >> M;
    int x,y;
    for(int i=1; i<=M; ++i)
    {
        fin >> x >> y;
        G[x].pb(y);
        TG[y].pb(x);
    }
}

void FrontDFS(int node)
{
    used[node] = true;
    for(int it : G[node])
        if(!used[it])
            FrontDFS(it);
    Kos_st.push(node);
}

void BackDFS(int node)
{
    ctc[node] = ctc_sz;
    for(int it : TG[node])
        if(!ctc[it])
            BackDFS(it);
}
// Till here, it's all cool xD :d
void Kosaraju()
{
    for(int i=1;i<=N;++i)
        if(!used[i])
            FrontDFS(i);

    while(!Kos_st.empty())
    {
        int node = Kos_st.top();
        Kos_st.pop();
        if(!ctc[node])
        {
            ++ctc_sz;
            BackDFS(node);
        }
    }
}

void CTCDFS(int node)
{
    used[node] = true;
    rmq[0][++euler_sz] = node;
    first[node] = euler_sz;
    for(int it : CTC_G[node])
        if(!used[it])
        {
            CTCDFS(it);
            rmq[0][++euler_sz] = node;
            anc[0][node] = min(anc[0][it],anc[0][node]); // how high can we get ?
        }
}

void BuildTree()
{
    memset(anc[0],0x3f,sizeof anc[0] );
    for(int x=1; x<=N; ++x)
        for(int y : G[x])
            if(ctc[x] != ctc[y])
            {
                CTC_G[ctc[x]].insert(ctc[y]);
                anc[0][ctc[y]] = min(anc[0][ctc[y]],ctc[x]);
            }
    memset(used, 0, sizeof used);
    CTCDFS(1);
}

void ComputeLb()
{
    for(int i=2; i <= euler_sz; ++i)
        lb[i] = lb[i>>1] + 1;
}

void ComputeDynamics()
{
    for(int i=1; i <= lb[euler_sz]; ++i)
    {
        for(int j=1; j <= ctc_sz; ++j)
                anc[i][j] = anc[i-1][anc[i-1][j]];
        for(int j=1; j <= euler_sz - (1<<i) + 1; ++j)
            rmq[i][j] = min(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);
    }
}

int LCA(int a, int b)
{
    a = first[a];
    b = first[b];
    if(a>b) swap(a,b);

    int len = lb[b-a];
    return min(rmq[len][a], rmq[len][b - (1<<len) + 1]);
}

void SolveQueries()
{
    int Q;
    fin >> Q;
    while(Q--)
    {
        int x,y,L,ans = 1;
        fin >> x >> y;
        x = ctc[x];
        y = ctc[y];
        L = LCA(x,y);

        if(x == L)
        {
            fout << "0\n";
            continue;
        }

        // go up.
        for(int i = lb[ctc_sz]; i>=0; --i)
        {
            if(anc[i][x] > L)
            {
                x = anc[i][x];
                ans += (1<<i);
            }
        }
        fout << ans << '\n';
    }
}

void Solve()
{
    ReadInput();
    Kosaraju();
    ComputeLb();
    BuildTree();
    ComputeDynamics();
    SolveQueries();
}

int main()
{
    Solve();
    return 0;
}