Pagini recente » Cod sursa (job #3124129) | Cod sursa (job #2378730) | Cod sursa (job #268738) | Istoria paginii runda/dedicatie_speciala/clasament | Cod sursa (job #3225044)
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#endif
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;
using namespace std;
const int NMAX = 32005;
const int LOG_MAX = 15;
const int NMIN = 2005;
/*******************************/
// INPUT / OUTPUT
#ifndef LOCAL
ifstream in("obiective.in");
ofstream out("obiective.out");
#define cin in
#define cout out
#endif
/*******************************/
/// GLOBAL DECLARATIONS
int N, M, Q;
int comps, rad;
int dp[LOG_MAX][NMAX];
int vis[NMAX], culoare[NMAX], h[NMAX], dist[NMAX], ins[NMAX], par_a[NMAX], par_b[NMAX];
int tata[LOG_MAX][NMAX];
vector <int> ord;
vector <int> adj[NMAX], inv_adj[NMAX], cg[NMAX], g[NMAX], nebune[NMAX], inv_cg[NMAX];
/*******************************/
/// FUNCTIONS
void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
cin >> N >> M;
int a, b;
for (int i = 0 ; i < M ; ++ i)
{
cin >> a >> b;
adj[a].push_back(b);
inv_adj[b].push_back(a);
}
cin >> Q;
}
///-------------------------------------
inline void dfs1(int node)
{
if (vis[node]) return;
vis[node] = true;
for (auto u: adj[node])
dfs1(u);
ord.push_back(node);
}
///-------------------------------------
inline void dfs2(int node)
{
if (culoare[node]) return;
culoare[node] = comps;
for (auto u: inv_adj[node])
dfs2(u);
}
///-------------------------------------
inline void dfs3(int node, int par)
{
vis[node] = true;
for (auto u: cg[node])
{
if (u == par || vis[u]) continue;
dfs3(u, node);
}
ord.push_back(node);
}
///-------------------------------------
inline int lca(int a, int b)
{
if (h[a] < h[b]) swap(a, b);
int dt = h[a] - h[b];
for (int put = LOG_MAX - 1 ; put >= 0 ; -- put)
{
if (dt & (1 << put))
a = tata[put][a];
}
if (a == b) return a;
for (int put = LOG_MAX - 1 ; put >= 0 ; -- put)
{
if (tata[put][a] != tata[put][b])
{
a = tata[put][a];
b = tata[put][b];
}
}
a = tata[0][a];
return a;
}
///-------------------------------------
inline void dfs(int node)
{
dp[0][node] = tata[0][node];
for (auto u: nebune[node])
{
if (h[dp[0][node]] > h[u])
dp[0][node] = u;
}
for (auto u: g[node])
{
dfs(u);
if (h[dp[0][node]] > h[dp[0][u]])
dp[0][node] = dp[0][u];
}
}
///-------------------------------------
inline void Solution()
{
for (int i = 1 ; i <= N ; ++ i)
dfs1(i);
reverse(ord.begin(), ord.end());
for (auto node: ord)
{
if (!culoare[node])
{
++ comps;
dfs2(node);
}
}
map <pii, bool> mapa;
for (int i = 1 ; i <= N ; ++ i)
{
for (auto j: adj[i])
{
if (culoare[i] == culoare[j]) continue;
if (!mapa[{culoare[i], culoare[j]}])
{
mapa[{culoare[i], culoare[j]}] = true;
cg[culoare[i]].push_back(culoare[j]);
inv_cg[culoare[j]].push_back(culoare[i]);
ins[culoare[j]] ++;
}
}
}
for (int i = 1 ; i <= comps ; ++ i)
if (!ins[i])
rad = i;
h[0] = -1;
ord.clear();
memset(vis, false, sizeof(vis));
dfs3(rad, 0);
reverse(ord.begin(), ord.end());
for (auto node: ord)
{
for (auto u: cg[node])
{
tata[0][u] = node;
}
h[node] = h[tata[0][node]] + 1;
}
for (int i = 1 ; i <= comps ; ++ i)
{
if (tata[0][i] != 0)
{
g[tata[0][i]].push_back(i);
// cout << i << " are tatal " << tata[0][i] << "\n";
}
for (auto u: cg[i])
{
if (tata[0][u] != i)
{
nebune[u].push_back(i);
// cout << "nebunele lui " << u << ": " << i << "\n";
}
}
}
dfs(rad);
for (int put = 1 ; put < LOG_MAX ; ++ put)
{
for (int i = 1 ; i <= comps ; ++ i)
{
tata[put][i] = tata[put - 1][tata[put - 1][i]];
dp[put][i] = dp[put - 1][dp[put - 1][i]];
}
}
int a, b, stramos;
while (Q --)
{
cin >> a >> b;
a = culoare[a]; b = culoare[b];
stramos = lca(a, b);
if (stramos == a)
{
cout << "0\n";
}
else
{
int sol = 0;
for (int put = LOG_MAX - 1 ; put >= 0 ; -- put)
{
if (h[dp[put][a]] > h[stramos])
{
a = dp[put][a];
sol += (1 << put);
}
}
sol ++;
cout << sol << "\n";
}
}
}
///-------------------------------------
inline void Output()
{
}
///-------------------------------------
int main()
{
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
ReadInput();
Solution();
Output();
return 0;
}