Cod sursa(job #3271482)

Utilizator solicasolica solica solica Data 26 ianuarie 2025 12:46:38
Problema Obiective Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.07 kb
/*
  ____   ___  _     ___ ____    _
 / ___| / _ \| |   |_ _/ ___|  / \
 \___ \| | | | |    | | |     / _ \
  ___) | |_| | |___ | | |___ / ___ \
 |____/ \___/|_____|___\____/_/   \_\

*/
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define int long long int
#define pii pair<int,int>

const int NMAX = 32e3+9;

const int MOD = 1e9+7;

const int INF = 1e9+9;

int binpow(int n, int k)
{
    if (k==0)
    {
        return 1;
    }
    int x=binpow(n,k/2)%MOD;
    if (k%2==0)
    {
        return x*x%MOD;
    }
    else
    {
        return x*x%MOD*n%MOD;
    }
}

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

#define cin fin
#define cout fout

int n,m,i,j,q,a,b;

int ctc;

int whichcomp[NMAX];

vector<int>g[NMAX],comp[NMAX],s,gt[NMAX];

vector<pair<int,bool>>gcomp[NMAX];

bool viz[NMAX];

int cost[NMAX];

priority_queue<pii,vector<pii>,greater<pii>>pq;

void dijkstra (int startnode)
{
    for (int i=1; i<=n; i++)
    {
        cost[i]=INF;
    }
    cost[startnode]=0;
    pq.push ({0,startnode});
    while (!pq.empty())
    {
        int node=pq.top().second,currentcost=pq.top().first,edgecost;
        pq.pop();
        for (auto it : gcomp[node])
        {
            if (it.second==0)
            {
                edgecost=1;
            }
            else
            {
                edgecost=0;
            }
            if (cost[it.first]>currentcost+edgecost)
            {
                cost[it.first]=currentcost+edgecost;
                pq.push ({cost[it.first],it.first});
            }
        }
    }
}

void dfs1 (int node)
{
    viz[node]=1;
    for (auto it : g[node])
    {
        if (!viz[it])
        {
            dfs1(it);
        }
    }
    s.pb (node);
}

void dfs2(int node)
{
    viz[node]=1;
    comp[ctc].pb (node);
    whichcomp[node]=ctc;
    for (auto it : gt[node])
    {
        if (!viz[it])
        {
            dfs2(it);
        }
    }
}

void run_case ()
{
    cin>>n>>m;
    for (i=1; i<=m; i++)
    {
        cin>>a>>b;
        g[a].pb (b);
    }
    for (i=1; i<=n; i++)
    {
        if (!viz[i])
        {
            dfs1(i);
        }
    }
    for (i=1; i<=n; i++)
    {
        viz[i]=0;
    }
    reverse(s.begin(),s.end());
    for (auto it : s)
    {
        if (!viz[it])
        {
            ctc++;
            dfs2(it);
        }
    }
    for (i=1; i<=n; i++)
    {
        for (auto vec : g[i])
        {
            if (whichcomp[i]!=whichcomp[vec])
            {
                gcomp[whichcomp[i]].pb ({whichcomp[vec],1});//1 for true edge
                gcomp[whichcomp[vec]].pb ({whichcomp[i],0});//0 for fake edge
            }
        }
    }
    cin>>q;
    while (q--)
    {
        cin>>a>>b;
        dijkstra(whichcomp[a]);
        cout<<cost[whichcomp[b]]<<'\n';
    }
}

signed main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL),cout.tie ();
    int teste;
    teste=1;
    while (teste--)
    {
        run_case();
    }
}