110082024-06-04 12:26:39UVinceÚtvonalakcpp17Accepted 100/100564ms109200 KiB
#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";
    }
    

}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted72ms75804 KiB
2Accepted150ms80224 KiB
subtask215/15
3Accepted93ms75580 KiB
4Accepted94ms75808 KiB
5Accepted100ms77084 KiB
6Accepted93ms76132 KiB
7Accepted112ms78076 KiB
8Accepted166ms80384 KiB
subtask315/15
9Accepted75ms75876 KiB
10Accepted101ms75920 KiB
11Accepted97ms76884 KiB
12Accepted104ms77364 KiB
13Accepted148ms83860 KiB
14Accepted275ms92060 KiB
15Accepted479ms109200 KiB
subtask415/15
16Accepted92ms76024 KiB
17Accepted101ms75896 KiB
18Accepted90ms76228 KiB
19Accepted134ms78052 KiB
20Accepted168ms80352 KiB
subtask515/15
21Accepted93ms75988 KiB
22Accepted94ms75660 KiB
23Accepted79ms76152 KiB
24Accepted93ms75688 KiB
25Accepted82ms75748 KiB
26Accepted75ms75748 KiB
subtask640/40
27Accepted75ms75664 KiB
28Accepted194ms80268 KiB
29Accepted93ms75580 KiB
30Accepted94ms75808 KiB
31Accepted100ms77084 KiB
32Accepted93ms76132 KiB
33Accepted112ms78076 KiB
34Accepted166ms80384 KiB
35Accepted75ms75876 KiB
36Accepted101ms75920 KiB
37Accepted97ms76884 KiB
38Accepted104ms77364 KiB
39Accepted148ms83860 KiB
40Accepted275ms92060 KiB
41Accepted479ms109200 KiB
42Accepted92ms76024 KiB
43Accepted101ms75896 KiB
44Accepted90ms76228 KiB
45Accepted134ms78052 KiB
46Accepted168ms80352 KiB
47Accepted93ms75988 KiB
48Accepted94ms75660 KiB
49Accepted79ms76152 KiB
50Accepted93ms75688 KiB
51Accepted82ms75748 KiB
52Accepted75ms75748 KiB
53Accepted564ms108936 KiB
54Accepted75ms75812 KiB
55Accepted79ms76900 KiB
56Accepted89ms76260 KiB
57Accepted153ms78308 KiB
58Accepted142ms78316 KiB
59Accepted483ms109100 KiB
60Accepted216ms80608 KiB
61Accepted222ms80868 KiB
62Accepted182ms80924 KiB