107492024-04-11 08:10:14szilÚtvonalakcpp17Accepted 100/100294ms137884 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 400'001;
const int MAXK = 20;

vector<int> g[MAXN], g2[MAXN];
vector<int> st;
vector<vector<int>> comp;
int tin2[MAXN], low[MAXN], siz[MAXN], bcc[MAXN], timer = 1, nodes = 0, up[MAXN][MAXK], pref[MAXN], tin[MAXN], tout[MAXN], fok[MAXN];
bool is_art[MAXN];

void dfs(int u, int p = -1) {
    tin2[u] = low[u] = timer++;
    st.emplace_back(u);
    int cc = 0;
    for (int v : g[u]) {
        if (v == p) continue;
        if (tin2[v]) {
            low[u] = min(low[u], tin2[v]);
        } else {
            cc++;
            dfs(v, u);
            low[u] = min(low[u], low[v]);
            if (tin2[u] <= low[v]) {
                is_art[u] = (tin2[u] > 1 || tin2[v] > 2);
                comp.push_back({u});
                while (st.back() != v) {
                    comp.back().emplace_back(st.back());
                    st.pop_back();
                }
                st.pop_back();
                comp.back().emplace_back(v);
            }
        }
    }
}

void dfs2(int u, int p = -1) {
    tin[u] = timer++;
    pref[u] += siz[u];
    for (int v : g2[u]) {
        if (v != p) {
            pref[v] += pref[u];
            up[v][0] = u;
            dfs2(v, u);
        }
    }
    tout[u] = timer++;
}

bool is_ancestor(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int lca(int u, int v) {
    if (is_ancestor(u, v)) return u;
    if (is_ancestor(v, u)) return v;
    for (int k = MAXK - 1; k >= 0; k--) {
        if (up[u][k] && !is_ancestor(up[u][k], v)) {
            u = up[u][k];
        }
    }
    return up[u][0];
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, m, q; cin >> n >> m >> q;
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
        fok[u]++;
        fok[v]++;
    }
    dfs(1);
    for (int i = 1; i <= n; i++) {
        if (is_art[i]) {
            bcc[i] = ++nodes;
            siz[bcc[i]] = 1;
        }
    }

    vector<int> idx(MAXN);
    for (int i = 0; i < comp.size(); i++) {
        int t = ++nodes;
        idx[i] = t;
        for (int j : comp[i]) {
            if (!is_art[j]) {
                bcc[j] = t;
            }
        }
        siz[t] = comp[i].size() - 2;
    }

    for (int i = 0; i < comp.size(); i++) {
        for (int j : comp[i]) {
            if (is_art[j]) {
                g2[idx[i]].emplace_back(bcc[j]);
                g2[bcc[j]].emplace_back(idx[i]);
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        sort(g2[i].begin(), g2[i].end());
        g2[i].erase(unique(g2[i].begin(), g2[i].end()), g2[i].end());
    }

    timer = 1;
    dfs2(1);

/*
    for (int i = 1; i <= n; i++) {
        cerr << pref[bcc[i]] << " ";
    }
    cerr << endl;*/
    for (int k = 1; k < MAXK; k++) {
        for (int i = 1; i <= nodes; i++) {
            up[i][k] = up[up[i][k-1]][k-1];
        }
    }

    while (q--) {
        int u, v; cin >> u >> v;
        int nu = bcc[u], nv = bcc[v];
        int l = lca(nu, nv);
        int ans = pref[nu] + pref[nv] - 2 * pref[l] + siz[l];
        if (is_art[u]) ans--;
        if (is_art[v]) ans--;
        cout << ans << "\n";
        // cerr << "lca = " << lca(u, v) << endl;
    }
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted17ms42400 KiB
2Accepted86ms55484 KiB
subtask215/15
3Accepted17ms42936 KiB
4Accepted18ms43064 KiB
5Accepted21ms46428 KiB
6Accepted24ms44672 KiB
7Accepted54ms49944 KiB
8Accepted82ms56204 KiB
subtask315/15
9Accepted17ms44220 KiB
10Accepted19ms44620 KiB
11Accepted21ms46820 KiB
12Accepted32ms49304 KiB
13Accepted82ms73136 KiB
14Accepted155ms102452 KiB
15Accepted294ms128928 KiB
subtask415/15
16Accepted19ms44048 KiB
17Accepted19ms44432 KiB
18Accepted24ms45600 KiB
19Accepted50ms50720 KiB
20Accepted85ms56872 KiB
subtask515/15
21Accepted21ms44144 KiB
22Accepted18ms44400 KiB
23Accepted18ms45400 KiB
24Accepted17ms44748 KiB
25Accepted17ms44676 KiB
26Accepted17ms44820 KiB
subtask640/40
27Accepted21ms44724 KiB
28Accepted86ms57368 KiB
29Accepted17ms42936 KiB
30Accepted18ms43064 KiB
31Accepted21ms46428 KiB
32Accepted24ms44672 KiB
33Accepted54ms49944 KiB
34Accepted82ms56204 KiB
35Accepted17ms44220 KiB
36Accepted19ms44620 KiB
37Accepted21ms46820 KiB
38Accepted32ms49304 KiB
39Accepted82ms73136 KiB
40Accepted155ms102452 KiB
41Accepted294ms128928 KiB
42Accepted19ms44048 KiB
43Accepted19ms44432 KiB
44Accepted24ms45600 KiB
45Accepted50ms50720 KiB
46Accepted85ms56872 KiB
47Accepted21ms44144 KiB
48Accepted18ms44400 KiB
49Accepted18ms45400 KiB
50Accepted17ms44748 KiB
51Accepted17ms44676 KiB
52Accepted17ms44820 KiB
53Accepted273ms133612 KiB
54Accepted19ms45044 KiB
55Accepted21ms48284 KiB
56Accepted26ms46444 KiB
57Accepted64ms51464 KiB
58Accepted57ms52820 KiB
59Accepted289ms137884 KiB
60Accepted119ms57096 KiB
61Accepted123ms58600 KiB
62Accepted120ms59644 KiB