#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";
}
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 72ms | 75804 KiB | ||||
2 | Elfogadva | 150ms | 80224 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Elfogadva | 93ms | 75580 KiB | ||||
4 | Elfogadva | 94ms | 75808 KiB | ||||
5 | Elfogadva | 100ms | 77084 KiB | ||||
6 | Elfogadva | 93ms | 76132 KiB | ||||
7 | Elfogadva | 112ms | 78076 KiB | ||||
8 | Elfogadva | 166ms | 80384 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Elfogadva | 75ms | 75876 KiB | ||||
10 | Elfogadva | 101ms | 75920 KiB | ||||
11 | Elfogadva | 97ms | 76884 KiB | ||||
12 | Elfogadva | 104ms | 77364 KiB | ||||
13 | Elfogadva | 148ms | 83860 KiB | ||||
14 | Elfogadva | 275ms | 92060 KiB | ||||
15 | Elfogadva | 479ms | 109200 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Elfogadva | 92ms | 76024 KiB | ||||
17 | Elfogadva | 101ms | 75896 KiB | ||||
18 | Elfogadva | 90ms | 76228 KiB | ||||
19 | Elfogadva | 134ms | 78052 KiB | ||||
20 | Elfogadva | 168ms | 80352 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Elfogadva | 93ms | 75988 KiB | ||||
22 | Elfogadva | 94ms | 75660 KiB | ||||
23 | Elfogadva | 79ms | 76152 KiB | ||||
24 | Elfogadva | 93ms | 75688 KiB | ||||
25 | Elfogadva | 82ms | 75748 KiB | ||||
26 | Elfogadva | 75ms | 75748 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Elfogadva | 75ms | 75664 KiB | ||||
28 | Elfogadva | 194ms | 80268 KiB | ||||
29 | Elfogadva | 93ms | 75580 KiB | ||||
30 | Elfogadva | 94ms | 75808 KiB | ||||
31 | Elfogadva | 100ms | 77084 KiB | ||||
32 | Elfogadva | 93ms | 76132 KiB | ||||
33 | Elfogadva | 112ms | 78076 KiB | ||||
34 | Elfogadva | 166ms | 80384 KiB | ||||
35 | Elfogadva | 75ms | 75876 KiB | ||||
36 | Elfogadva | 101ms | 75920 KiB | ||||
37 | Elfogadva | 97ms | 76884 KiB | ||||
38 | Elfogadva | 104ms | 77364 KiB | ||||
39 | Elfogadva | 148ms | 83860 KiB | ||||
40 | Elfogadva | 275ms | 92060 KiB | ||||
41 | Elfogadva | 479ms | 109200 KiB | ||||
42 | Elfogadva | 92ms | 76024 KiB | ||||
43 | Elfogadva | 101ms | 75896 KiB | ||||
44 | Elfogadva | 90ms | 76228 KiB | ||||
45 | Elfogadva | 134ms | 78052 KiB | ||||
46 | Elfogadva | 168ms | 80352 KiB | ||||
47 | Elfogadva | 93ms | 75988 KiB | ||||
48 | Elfogadva | 94ms | 75660 KiB | ||||
49 | Elfogadva | 79ms | 76152 KiB | ||||
50 | Elfogadva | 93ms | 75688 KiB | ||||
51 | Elfogadva | 82ms | 75748 KiB | ||||
52 | Elfogadva | 75ms | 75748 KiB | ||||
53 | Elfogadva | 564ms | 108936 KiB | ||||
54 | Elfogadva | 75ms | 75812 KiB | ||||
55 | Elfogadva | 79ms | 76900 KiB | ||||
56 | Elfogadva | 89ms | 76260 KiB | ||||
57 | Elfogadva | 153ms | 78308 KiB | ||||
58 | Elfogadva | 142ms | 78316 KiB | ||||
59 | Elfogadva | 483ms | 109100 KiB | ||||
60 | Elfogadva | 216ms | 80608 KiB | ||||
61 | Elfogadva | 222ms | 80868 KiB | ||||
62 | Elfogadva | 182ms | 80924 KiB |