Cod sursa(job #3271351)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 25 ianuarie 2025 19:24:09
Problema Obiective Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.05 kb
#include <fstream>
#include <bits/stdc++.h>

using namespace std;
ifstream fin("obiective.in");
ofstream fout("obiective.out");
int n, m, i, j, gri[32005], nr, comp[32005], x, y, dist[32005], t, lca, poz[32005], depth[32005];
vector <int> v[32005], vt[32005], component[32005], g[32005];
stack <int> st;
queue <int> q;
vector <pair<int, int>> eulertour;
struct rangemin
{
    int nod, depth;
}rmq[21][32005];
bool viz[32005];
void etour(int nod)
{
    viz[nod]=1;
    eulertour.push_back({nod, depth[nod]});
    poz[nod]=eulertour.size();
    for(auto i: g[nod])
        if(viz[i]==0)
        {
            depth[i]=depth[nod]+1;
            etour(i);
            eulertour.push_back({nod, depth[nod]});
        }
}
void dfs(int nod)
{
    viz[nod]=1;
    for(auto i: v[nod])
        if(viz[i]==0)
            dfs(i);
    st.push(nod);
}
void dfs1(int nod)
{
    viz[nod]=1;
    component[nr].push_back(nod);
    comp[nod]=nr;
    for(auto i: vt[nod])
        if(viz[i]==0)
            dfs1(i);
}

int main()
{
    fin>>n>>m;
    for(i=1; i<=m; i++)
    {
        fin>>x>>y;
        v[x].push_back(y);
        vt[y].push_back(x);
    }
    dfs(1);
    for(i=1; i<=n; i++)
        viz[i]=0;
    while(!st.empty())
    {
        int x=st.top();
        st.pop();
        if(viz[x]==0)
        {
            nr++;
            dfs1(x);
        }
    }
    for(i=1; i<=n; i++)
        for(auto j: v[i])
            if(comp[i]!=comp[j])
            {
                gri[comp[j]]++;
                g[comp[i]].push_back(comp[j]);
            }
    int root;
    for(i=1; i<=nr; i++)
    {
       if(gri[i]==0)
        {
            q.push(i);
            root=i;
        }
        viz[i]=0;
    }
    etour(root);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(auto i: g[x])
        {
            gri[i]--;
            if(gri[i]==0)
            {
                dist[i]=dist[x]+1;
                q.push(i);
            }

        }
    }
    int nr=0;
    for(auto i: eulertour)
    {
       // fout<<i.first<<" "<<i.second<<'\n';
        nr++;
        rmq[0][nr].nod=i.first;
        rmq[0][nr].depth=i.second;
    }
    int p=2, k=0;
    while(p<=nr)
    {
        k++;
        for(i=1; i<=nr-p+1; i++)
            if(rmq[k-1][i].depth<rmq[k-1][i+p/2].depth)
        {
            rmq[k][i].depth=rmq[k-1][i].depth;
            rmq[k][i].nod=rmq[k-1][i].nod;
        }
        else
        {
            rmq[k][i].depth=rmq[k-1][i+p/2].depth;
            rmq[k][i].nod=rmq[k-1][i+p/2].nod;
        }
        p*=2;
    }
    fin>>t;
    while(t--)
    {
        fin>>x>>y;
        x=comp[x];
        y=comp[y];
        int st=min(poz[x], poz[y]);
        int dr=max(poz[x], poz[y]);
        int lg=dr-st+1;
        p=log2(lg);
        int pow2=(1<<p);
        if(rmq[p][st].depth<rmq[p][dr-pow2+1].depth)
            lca=rmq[p][st].nod;
        else
            lca=rmq[p][dr-pow2+1].nod;
        fout<<dist[x]-dist[lca]<<'\n';
    }
    return 0;
}