Cod sursa(job #2751354)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 14 mai 2021 20:30:18
Problema Obiective Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.16 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define nozerous(x) (x & -x)
#define MOD 1000000007
#define M_PI           3.14159265358979323846
#define EPS 0.00001
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = (1 << 30), NMAX(32005), VMAX(1000000);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using Point = array<int, 2>;
using ll = long long;
using cd = complex<double>;
const double PI = acos(-1);
void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
}
vector<int> G[NMAX], GT[NMAX], ctc[NMAX];
set<pair<int, int> > s;
bool viz[NMAX];
int comp[NMAX], nr, grad[NMAX], dp[17][NMAX], dp2[17][NMAX], lvl[NMAX];
stack<int> st;
void DFS(int nod)
{
    viz[nod] = 1;
    for(auto it: G[nod])
        if(!viz[it])
            DFS(it);
    st.push(nod);
}
void DFS2(int nod)
{
    viz[nod] = 1;
    comp[nod] = nr;
    for(auto it: GT[nod])
        if(!viz[it])
            DFS2(it);
}
void DFSLCA(int nod, int tata)
{
    dp2[0][nod] = tata;
    dp[0][nod] = tata;
    lvl[nod] = lvl[tata] + 1;
    viz[nod] = 1;
    for(auto it: ctc[nod])
        if(!viz[it])
            DFSLCA(it, nod);
        else if(lvl[dp2[0][it]] > lvl[nod])
            dp[0][it] = nod;
}
void CALC(int nod, int tata)
{
    viz[nod] = 1;
    for(auto it: ctc[nod])
        if(!viz[it])
        {
            CALC(it, nod);
            if(lvl[dp2[0][nod]] > lvl[dp2[0][it]])
                dp2[0][nod] = dp2[0][it];
        }
}
int lca(int x, int y)
{
    if(lvl[x] > lvl[y])
        swap(x, y);
    int difN = lvl[y] - lvl[x];
    for(int i = 16; i >= 0; --i)
        if(difN & (1 << i))
            y = dp[i][y];
    if(x == y)
        return y;
    for(int i = 16; i >= 0; --i)
        if(dp[i][x] != dp[i][y])
        {
            x = dp[i][x];
            y = dp[i][y];
        }
    return dp[0][y];
}
int main()
{
    BUNA("obiective");
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        GT[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i)
        if(!viz[i])
            DFS(i);
    memset(viz, 0, sizeof(viz));
    while(!st.empty())
    {
        if(viz[st.top()] == 0)
        {
            ++nr;
            DFS2(st.top());
        }
        st.pop();
    }
    for(int i = 1; i <= n; ++i)
        for(auto it: G[i])
            if(comp[i] != comp[it] && s.find({comp[i], comp[it]}) == s.end())
            {
                s.insert({comp[i], comp[it]});
                s.insert({comp[it], comp[i]});
                ctc[comp[i]].push_back(comp[it]);
                grad[comp[it]]++;
            }
    ///momentan atat am avut idee
    for(int i = 1; i <= nr; ++i)
        sort(ctc[i].begin(), ctc[i].end());
    int root = 0;
    for(int i = 1; i <= nr; ++i)
        if(grad[i] == 0)
            root = i;
    ///am creat arborele de comp conexe
    memset(viz, 0, sizeof(viz));
    DFSLCA(root, 0);
    memset(viz, 0, sizeof(viz));
    CALC(root, 0);
    for(int i = 1; (1 << i) <= nr; ++i)
        for(int j = 1; j <= n; ++j)
        {
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
            dp2[i][j] = dp2[i - 1][dp2[i - 1][j]];
        }
    ///niste binary lifting ca de ce nu
    int t;
    cin >> t;
    while(t--)
    {
        int x, y;
        cin >> x >> y;
        x = comp[x];
        y = comp[y];
        if(x == y)
        {
            cout << 0 << '\n';
            continue;
        }
        int l = lca(x, y);
        if(x == l)
        {
            cout << 0 << '\n';
            continue;
        }
        int rez = 0;
        for(int i = 16; i >= 0; --i)
            if(dp2[i][x] != 0 && lvl[dp2[i][x]] > lvl[l])
            {
                rez += (1 << i);
                x = dp2[i][x];
            }
        cout << rez + 1 << '\n';
    }
    PA();
}