#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef CB
freopen("be1.txt","r",stdin);
// freopen("ki.txt","w",stdout);
#endif
int N,M,K;
cin>>N>>M>>K;
vector<vector<int>>g(N+1);
for(int i=0;i<M;i++){
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int>v(N+1),w(N+1),p(N+1),a(N+1);
vector<vector<int>>c(N+1),cc;
auto dfs=[&](auto self,int i)->unordered_set<int>{
unordered_set<int>s;
w[i]=v[i];
for(int j:g[i]){
if(j==p[i]){
continue;
}
if(v[j]){
if(v[j]<v[i]){
w[i]=min(w[i],v[j]);
}
continue;
}
v[j]=v[i]+1;
p[j]=i;
auto z=self(self,j);
if(w[j]>=v[i]){
a[i]=1;
z.insert(i);
for(int k:z){
c[k].push_back(cc.size());
}
cc.emplace_back(z.begin(),z.end());
}
else{
w[i]=min(w[i],w[j]);
if(s.size()<z.size()){
swap(s,z);
}
s.insert(z.begin(),z.end());
}
}
s.insert(i);
return s;
};
v[1]=1;
dfs(dfs,1);
vector<vector<int>>q(N+1);
for(int i=0;i<K;i++){
int a,b;
cin>>a>>b;
q[a].push_back(i);
q[b].push_back(i);
}
vector<int>ans(K,-1);
auto dfs2=[&](auto self,int i,int p,int d)->unordered_map<int,int>{
unordered_map<int,int>s;
for(int x:q[i]){
s[x]=d;
}
for(int j:c[i]){
if(j==p){
continue;
}
int dd=d+cc[j].size()-1;
unordered_map<int,int>t;
for(int k:cc[j]){
if(k==i||!a[k]){
continue;
}
auto z=self(self,k,j,dd);
if(t.size()<z.size()){
swap(t,z);
}
for(auto[x,y]:z){
if(t.count(x)){
ans[x]=t[x]+y+cc[j].size()-dd*2;
t.erase(x);
}
else{
t[x]=y;
}
}
}
for(int k:cc[j]){
if(k==i||a[k]){
continue;
}
for(int x:q[k]){
if(t.count(x)){
ans[x]=t[x]+1-d;
t.erase(x);
}
else{
t[x]=dd;
}
}
}
if(s.size()<t.size()){
swap(s,t);
}
for(auto[x,y]:t){
if(s.count(x)){
ans[x]=s[x]+y+1-d*2;
s.erase(x);
}
else{
s[x]=y;
}
}
}
return s;
};
dfs2(dfs2,1,-1,0);
for(int i:ans){
cout<<i-2<<'\n';
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1832 KiB | ||||
2 | Accepted | 190ms | 44496 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 4ms | 2748 KiB | ||||
4 | Accepted | 4ms | 3464 KiB | ||||
5 | Accepted | 7ms | 6232 KiB | ||||
6 | Accepted | 14ms | 7140 KiB | ||||
7 | Accepted | 79ms | 23972 KiB | ||||
8 | Accepted | 163ms | 45212 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 4ms | 3900 KiB | ||||
10 | Accepted | 4ms | 4636 KiB | ||||
11 | Accepted | 8ms | 7432 KiB | ||||
12 | Accepted | 14ms | 8244 KiB | ||||
13 | Accepted | 79ms | 28396 KiB | ||||
14 | Accepted | 173ms | 54312 KiB | ||||
15 | Accepted | 321ms | 116884 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Accepted | 6ms | 5180 KiB | ||||
17 | Accepted | 8ms | 5948 KiB | ||||
18 | Accepted | 14ms | 8744 KiB | ||||
19 | Accepted | 82ms | 25316 KiB | ||||
20 | Accepted | 188ms | 46572 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Accepted | 3ms | 4496 KiB | ||||
22 | Accepted | 3ms | 4404 KiB | ||||
23 | Accepted | 4ms | 5500 KiB | ||||
24 | Accepted | 3ms | 4660 KiB | ||||
25 | Accepted | 4ms | 5068 KiB | ||||
26 | Accepted | 4ms | 5180 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Accepted | 3ms | 4796 KiB | ||||
28 | Accepted | 171ms | 47068 KiB | ||||
29 | Accepted | 4ms | 2748 KiB | ||||
30 | Accepted | 4ms | 3464 KiB | ||||
31 | Accepted | 7ms | 6232 KiB | ||||
32 | Accepted | 14ms | 7140 KiB | ||||
33 | Accepted | 79ms | 23972 KiB | ||||
34 | Accepted | 163ms | 45212 KiB | ||||
35 | Accepted | 4ms | 3900 KiB | ||||
36 | Accepted | 4ms | 4636 KiB | ||||
37 | Accepted | 8ms | 7432 KiB | ||||
38 | Accepted | 14ms | 8244 KiB | ||||
39 | Accepted | 79ms | 28396 KiB | ||||
40 | Accepted | 173ms | 54312 KiB | ||||
41 | Accepted | 321ms | 116884 KiB | ||||
42 | Accepted | 6ms | 5180 KiB | ||||
43 | Accepted | 8ms | 5948 KiB | ||||
44 | Accepted | 14ms | 8744 KiB | ||||
45 | Accepted | 82ms | 25316 KiB | ||||
46 | Accepted | 188ms | 46572 KiB | ||||
47 | Accepted | 3ms | 4496 KiB | ||||
48 | Accepted | 3ms | 4404 KiB | ||||
49 | Accepted | 4ms | 5500 KiB | ||||
50 | Accepted | 3ms | 4660 KiB | ||||
51 | Accepted | 4ms | 5068 KiB | ||||
52 | Accepted | 4ms | 5180 KiB | ||||
53 | Accepted | 324ms | 120048 KiB | ||||
54 | Accepted | 6ms | 5828 KiB | ||||
55 | Accepted | 8ms | 9380 KiB | ||||
56 | Accepted | 19ms | 9420 KiB | ||||
57 | Accepted | 116ms | 28812 KiB | ||||
58 | Accepted | 103ms | 29980 KiB | ||||
59 | Accepted | 397ms | 142520 KiB | ||||
60 | Accepted | 233ms | 52288 KiB | ||||
61 | Accepted | 256ms | 53196 KiB | ||||
62 | Accepted | 264ms | 53716 KiB |