110042024-06-04 11:33:32UVinceÚtvonalakcpp17Wrong answer 0/100333ms103628 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();
}

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 : g[comps[v][0]]){
            if (!bctvis[comp[to]]) bct(comp[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]+comps[c].size()-2<<"\n";
    }
    

}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted71ms75620 KiB
2Wrong answer152ms80224 KiB
subtask20/15
3Accepted75ms75752 KiB
4Wrong answer90ms75784 KiB
5Wrong answer76ms76588 KiB
6Accepted104ms76252 KiB
7Accepted130ms78072 KiB
8Accepted177ms80184 KiB
subtask30/15
9Wrong answer75ms76024 KiB
10Wrong answer82ms75900 KiB
11Wrong answer78ms76648 KiB
12Wrong answer82ms77152 KiB
13Wrong answer159ms83220 KiB
14Wrong answer264ms90764 KiB
15Wrong answer270ms101404 KiB
subtask40/15
16Wrong answer93ms76044 KiB
17Wrong answer101ms75908 KiB
18Wrong answer90ms76264 KiB
19Accepted136ms78052 KiB
20Wrong answer160ms80392 KiB
subtask50/15
21Accepted75ms75676 KiB
22Accepted90ms75620 KiB
23Wrong answer75ms75876 KiB
24Wrong answer82ms75636 KiB
25Wrong answer89ms75748 KiB
26Wrong answer75ms75748 KiB
subtask60/40
27Accepted75ms75620 KiB
28Wrong answer202ms80256 KiB
29Accepted75ms75752 KiB
30Wrong answer90ms75784 KiB
31Wrong answer76ms76588 KiB
32Accepted104ms76252 KiB
33Accepted130ms78072 KiB
34Accepted177ms80184 KiB
35Wrong answer75ms76024 KiB
36Wrong answer82ms75900 KiB
37Wrong answer78ms76648 KiB
38Wrong answer82ms77152 KiB
39Wrong answer159ms83220 KiB
40Wrong answer264ms90764 KiB
41Wrong answer270ms101404 KiB
42Wrong answer93ms76044 KiB
43Wrong answer101ms75908 KiB
44Wrong answer90ms76264 KiB
45Accepted136ms78052 KiB
46Wrong answer160ms80392 KiB
47Accepted75ms75676 KiB
48Accepted90ms75620 KiB
49Wrong answer75ms75876 KiB
50Wrong answer82ms75636 KiB
51Wrong answer89ms75748 KiB
52Wrong answer75ms75748 KiB
53Wrong answer333ms101064 KiB
54Wrong answer76ms75880 KiB
55Wrong answer79ms76644 KiB
56Wrong answer90ms76260 KiB
57Wrong answer136ms78224 KiB
58Wrong answer153ms78536 KiB
59Wrong answer300ms103628 KiB
60Wrong answer197ms80680 KiB
61Wrong answer208ms80868 KiB
62Wrong answer193ms80920 KiB