学了一下圆方树, 好神奇的东西呀。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, q; vector<PII> G[N];
vector<int> G1[N];
int dfn[N], low[N], idx, nn;
int stk[N], top; void tarjan(int u, int preid) {
dfn[u] = low[u] = ++idx;
stk[++top] = u;
for(auto& e : G[u]) {
if(e.se == preid) continue;
int v = e.fi;
if(!dfn[v]) {
tarjan(v, e.se);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) {
nn++;
G1[u].push_back(nn);
G1[nn].push_back(u);
do {
G1[stk[top]].push_back(nn);
G1[nn].push_back(stk[top]);
} while(stk[top--] != v);
}
} else low[u] = min(low[u], dfn[v]);
}
} int f[N][], depth[N]; void dfs(int u, int fa) {
depth[u] = depth[fa] + ;
f[u][] = fa;
for(int i = ; i < ; i++)
f[u][i] = f[f[u][i - ]][i - ];
for(auto& v : G1[u]) {
if(v == fa) continue;
dfs(v, u);
}
} int getLca(int u, int v) {
if(depth[u] < depth[v]) swap(u, v);
for(int i = ; i >= ; i--)
if(depth[f[u][i]] >= depth[v])
u = f[u][i];
if(u == v) return u;
for(int i = ; i >= ; i--)
if(f[u][i] != f[v][i])
u = f[u][i], v = f[v][i];
return f[u][];
} int main() {
scanf("%d%d%d", &n, &m, &q);
nn = n;
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(mk(v, i));
G[v].push_back(mk(u, i));
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i, -);
dfs(, );
while(q--){
int u, v; scanf("%d%d", &u, &v);
int lca = getLca(u, v); printf("%d\n", (depth[u] + depth[v]) / - depth[lca]);
}
return ;
} /*
*/