#include <bits/stdc++.h>
using namespace std;
const int c=3e5+5;
const int ce=1e6+6;
int n,m,q,tt,K;
vector<int> adj[c];
bool volt[ce], vis[c];
int vag[c], t[c], l[c], cnt[c], dep[c];
int mp[c];
vector<vector<int>> comp;
vector<pair<int,int>> s;
vector<int> tree[c];
void dfs(int v, int p){
t[v]=l[v]=++tt;
int vag1=0;
for(int x:adj[v]){
if(x==p) continue;
if(!t[x]){
vag1++;
s.push_back(make_pair(v,x));
dfs(x,v);
l[v]=min(l[v],l[x]);
if(l[x]>=t[v] && p){
if(!vag[v]) vag[v]=++K;
comp.push_back(vector<int>());
while(!s.empty()){
int a=s.back().first,b=s.back().second;
comp.back().push_back(a);
comp.back().push_back(b);
s.pop_back();
if(a==v) break;
}
}
if(!p){
comp.push_back(vector<int>());
while(!s.empty()){
comp.back().push_back(s.back().first);
comp.back().push_back(s.back().second);
s.pop_back();
}
}
}
else l[v]=min(l[v],t[x]);
}
if(vag1>1 && !p) vag[1]=++K;
}
void build(){
for(int i=1;i<=n;i++){
if(vag[i]) {mp[i]=vag[i]+comp.size(); cnt[mp[i]]=-1;}
}
for(int i=0;i<comp.size();i++){
vector<int> cut;
sort(comp[i].begin(),comp[i].end());
comp[i].resize(unique(comp[i].begin(),comp[i].end())-comp[i].begin());
cnt[i+1]=comp[i].size();
for(int x:comp[i])
{
if(!vag[x]) mp[x]=i+1;
else {
cut.push_back(x);
tree[i+1].push_back(mp[x]);
tree[mp[x]].push_back(i+1);
}
}
}
}
int up[c][19];
int sum[c][19];
void dfs2(int v, int p){
up[v][0]=p;
sum[v][0]=cnt[v];
for(int i=1;i<19;i++){
int x = up[v][i-1];
up[v][i]=up[x][i-1];
sum[v][i]=sum[v][i-1]+sum[x][i-1];
}
for(int x:tree[v]){
if(x != p){
dep[x]=dep[v]+1;
dfs2(x,v);
}
}
}
int query(int a, int b){
int res=(vag[a]>0) + (vag[b]>0);
a=mp[a], b=mp[b];
if(dep[a]>dep[b]) swap(a,b);
int k=dep[b]-dep[a];
for(int i=18;i>=0;i--){
if((k>>i)&1){
res+=sum[b][i];
b=up[b][i];
}
}
if(a==b){
res+=sum[a][0];
return res;
}
for(int i=18;i>=0;i--){
if(up[a][i] != up[b][i]){
res+=sum[a][i]+sum[b][i];
a=up[a][i], b=up[b][i];
}
}
res+=sum[up[a][0]][0]+sum[a][0]+sum[b][0];
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=m;i++){
int a,b; cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
build();
dfs2(1,0);
while(q--){
int a,b; cin>>a>>b;
cout << query(a,b)-2 << '\n';
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 14ms | 30128 KiB | ||||
2 | Accepted | 112ms | 44880 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 14ms | 30752 KiB | ||||
4 | Accepted | 17ms | 31236 KiB | ||||
5 | Accepted | 21ms | 35816 KiB | ||||
6 | Accepted | 24ms | 32812 KiB | ||||
7 | Accepted | 59ms | 38184 KiB | ||||
8 | Accepted | 108ms | 45728 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 17ms | 32252 KiB | ||||
10 | Accepted | 17ms | 33148 KiB | ||||
11 | Accepted | 21ms | 36260 KiB | ||||
12 | Accepted | 28ms | 39076 KiB | ||||
13 | Accepted | 93ms | 69888 KiB | ||||
14 | Accepted | 177ms | 108404 KiB | ||||
15 | Accepted | 330ms | 152876 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Accepted | 17ms | 32396 KiB | ||||
17 | Accepted | 18ms | 32784 KiB | ||||
18 | Accepted | 21ms | 34248 KiB | ||||
19 | Accepted | 61ms | 39656 KiB | ||||
20 | Accepted | 114ms | 46424 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Accepted | 14ms | 32344 KiB | ||||
22 | Accepted | 16ms | 32380 KiB | ||||
23 | Accepted | 17ms | 33632 KiB | ||||
24 | Accepted | 14ms | 32700 KiB | ||||
25 | Accepted | 14ms | 32804 KiB | ||||
26 | Accepted | 16ms | 32788 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Accepted | 14ms | 32612 KiB | ||||
28 | Accepted | 114ms | 47112 KiB | ||||
29 | Accepted | 14ms | 30752 KiB | ||||
30 | Accepted | 17ms | 31236 KiB | ||||
31 | Accepted | 21ms | 35816 KiB | ||||
32 | Accepted | 24ms | 32812 KiB | ||||
33 | Accepted | 59ms | 38184 KiB | ||||
34 | Accepted | 108ms | 45728 KiB | ||||
35 | Accepted | 17ms | 32252 KiB | ||||
36 | Accepted | 17ms | 33148 KiB | ||||
37 | Accepted | 21ms | 36260 KiB | ||||
38 | Accepted | 28ms | 39076 KiB | ||||
39 | Accepted | 93ms | 69888 KiB | ||||
40 | Accepted | 177ms | 108404 KiB | ||||
41 | Accepted | 330ms | 152876 KiB | ||||
42 | Accepted | 17ms | 32396 KiB | ||||
43 | Accepted | 18ms | 32784 KiB | ||||
44 | Accepted | 21ms | 34248 KiB | ||||
45 | Accepted | 61ms | 39656 KiB | ||||
46 | Accepted | 114ms | 46424 KiB | ||||
47 | Accepted | 14ms | 32344 KiB | ||||
48 | Accepted | 16ms | 32380 KiB | ||||
49 | Accepted | 17ms | 33632 KiB | ||||
50 | Accepted | 14ms | 32700 KiB | ||||
51 | Accepted | 14ms | 32804 KiB | ||||
52 | Accepted | 16ms | 32788 KiB | ||||
53 | Accepted | 252ms | 154032 KiB | ||||
54 | Accepted | 14ms | 33188 KiB | ||||
55 | Accepted | 18ms | 37576 KiB | ||||
56 | Accepted | 24ms | 34864 KiB | ||||
57 | Accepted | 78ms | 40316 KiB | ||||
58 | Accepted | 68ms | 41912 KiB | ||||
59 | Accepted | 333ms | 154584 KiB | ||||
60 | Accepted | 142ms | 46224 KiB | ||||
61 | Accepted | 143ms | 48288 KiB | ||||
62 | Accepted | 151ms | 49532 KiB |