#include <bits/stdc++.h>
using namespace std;
using ll=long long;
void solve();
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t=1;
//cin>>t;
while (t--) solve();
return 0;
}
const int maxn=4e5+1;
int n,m,k;
vector<vector<int>> g(maxn);
vector<int> t(maxn, -1),l(maxn, -1);
vector<bool> vis(maxn,false);
int timer=0;
stack<int> st;
vector<vector<int>> comps;
vector<int> art(maxn);
vector<int> comp(maxn,-1);
void dfs(int v, int p=-1){
t[v]=l[v]=++timer;
vis[v]=true;
st.push(v);
for (int to : g[v]){
if (p==to) continue;
if (vis[to]){
l[v]=min(l[v], t[to]);
}
else {
dfs(to, v);
l[v]=min(l[v], l[to]);
if (l[to]>=t[v]){
art[v]= (t[v]>1 || t[to]>2);
comps.push_back({v});
comp[v]=comps.size()-1;
while (comps.back().back()!=to){
comps.back().push_back(st.top());
comp[st.top()]=comps.size()-1;
st.pop();
}
}
}
}
}
vector<bool> bctvis(maxn,false);
vector<vector<int>> bctg(maxn);
void bct(int v){
bctvis[v]=true;
if (comps[v].size()==1){
for (int to : bctg[v]){
if (!bctvis[to]) bct(to);
}
}else {
for (int node : comps[v]){
if (art[node]){
bctg[v].push_back(comp[node]);
if (!bctvis[comp[node]]) bct(comp[node]);
}
}
}
}
vector<int> pr(maxn,1);
vector<vector<int>> binary(maxn, vector<int> (20, 0));
vector<int> layer(maxn, -1);
void pref(int v, int p=-1){
pr[v]=pr[max(p,0)]-1+comps[v].size();
layer[v]=layer[max(p,0)]+1;
if (p!=-1){
binary[v][0]=p;
for (int i=1;i<20;i++){
binary[v][i]=binary[binary[v][i-1]][i-1];
}
}
for (int to : bctg[v]){
if (to==p) continue;
pref(to,v);
}
}
int lca(int a,int b){
if (layer[a]<layer[b]) swap(a,b);
if (layer[a]>layer[b]){
for (int i=19;i>=0;i--){
int c=binary[a][i];
if (layer[c]<=layer[b]) continue;
a=c;
}
a=binary[a][0];
}
if (a==b) return a;
for (int i=19;i>=0;i--){
int c=binary[a][i];
int d=binary[b][i];
if (c==d) continue;
a=c;
b=d;
}
return binary[a][0];
}
void solve(){
cin>>n>>m>>k;
for (int i = 0; i < m; i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
for (int i=1;i<=n;i++){
if (art[i]){
comps.push_back({i});
comp[i]=comps.size()-1;
}
}
for (int i=0;i<comps.size();i++){
if (comps[i].size()==1) continue;
for (int j : comps[i]){
if (art[j]) bctg[comp[j]].push_back(i);
}
}
bct(0);
pref(0);
for (int i = 0; i < k; i++)
{
int a,b;
cin>>a>>b;
a=comp[a];
b=comp[b];
int c = lca(a,b);
cout<<pr[a]+pr[b]-2*pr[c]+int(comps[c].size())-2<<"\n";
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 72ms | 75804 KiB | ||||
2 | Accepted | 150ms | 80224 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 93ms | 75580 KiB | ||||
4 | Accepted | 94ms | 75808 KiB | ||||
5 | Accepted | 100ms | 77084 KiB | ||||
6 | Accepted | 93ms | 76132 KiB | ||||
7 | Accepted | 112ms | 78076 KiB | ||||
8 | Accepted | 166ms | 80384 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 75ms | 75876 KiB | ||||
10 | Accepted | 101ms | 75920 KiB | ||||
11 | Accepted | 97ms | 76884 KiB | ||||
12 | Accepted | 104ms | 77364 KiB | ||||
13 | Accepted | 148ms | 83860 KiB | ||||
14 | Accepted | 275ms | 92060 KiB | ||||
15 | Accepted | 479ms | 109200 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Accepted | 92ms | 76024 KiB | ||||
17 | Accepted | 101ms | 75896 KiB | ||||
18 | Accepted | 90ms | 76228 KiB | ||||
19 | Accepted | 134ms | 78052 KiB | ||||
20 | Accepted | 168ms | 80352 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Accepted | 93ms | 75988 KiB | ||||
22 | Accepted | 94ms | 75660 KiB | ||||
23 | Accepted | 79ms | 76152 KiB | ||||
24 | Accepted | 93ms | 75688 KiB | ||||
25 | Accepted | 82ms | 75748 KiB | ||||
26 | Accepted | 75ms | 75748 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Accepted | 75ms | 75664 KiB | ||||
28 | Accepted | 194ms | 80268 KiB | ||||
29 | Accepted | 93ms | 75580 KiB | ||||
30 | Accepted | 94ms | 75808 KiB | ||||
31 | Accepted | 100ms | 77084 KiB | ||||
32 | Accepted | 93ms | 76132 KiB | ||||
33 | Accepted | 112ms | 78076 KiB | ||||
34 | Accepted | 166ms | 80384 KiB | ||||
35 | Accepted | 75ms | 75876 KiB | ||||
36 | Accepted | 101ms | 75920 KiB | ||||
37 | Accepted | 97ms | 76884 KiB | ||||
38 | Accepted | 104ms | 77364 KiB | ||||
39 | Accepted | 148ms | 83860 KiB | ||||
40 | Accepted | 275ms | 92060 KiB | ||||
41 | Accepted | 479ms | 109200 KiB | ||||
42 | Accepted | 92ms | 76024 KiB | ||||
43 | Accepted | 101ms | 75896 KiB | ||||
44 | Accepted | 90ms | 76228 KiB | ||||
45 | Accepted | 134ms | 78052 KiB | ||||
46 | Accepted | 168ms | 80352 KiB | ||||
47 | Accepted | 93ms | 75988 KiB | ||||
48 | Accepted | 94ms | 75660 KiB | ||||
49 | Accepted | 79ms | 76152 KiB | ||||
50 | Accepted | 93ms | 75688 KiB | ||||
51 | Accepted | 82ms | 75748 KiB | ||||
52 | Accepted | 75ms | 75748 KiB | ||||
53 | Accepted | 564ms | 108936 KiB | ||||
54 | Accepted | 75ms | 75812 KiB | ||||
55 | Accepted | 79ms | 76900 KiB | ||||
56 | Accepted | 89ms | 76260 KiB | ||||
57 | Accepted | 153ms | 78308 KiB | ||||
58 | Accepted | 142ms | 78316 KiB | ||||
59 | Accepted | 483ms | 109100 KiB | ||||
60 | Accepted | 216ms | 80608 KiB | ||||
61 | Accepted | 222ms | 80868 KiB | ||||
62 | Accepted | 182ms | 80924 KiB |