110152024-06-04 17:27:42mraronÚtvonalakcpp17Accepted 100/100312ms67964 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
1Accepted21ms20836 KiB
2Accepted94ms27292 KiB
subtask215/15
3Accepted23ms20964 KiB
4Accepted19ms20968 KiB
5Accepted23ms22632 KiB
6Accepted28ms21604 KiB
7Accepted54ms24288 KiB
8Accepted82ms27276 KiB
subtask315/15
9Accepted18ms21288 KiB
10Accepted25ms21420 KiB
11Accepted28ms22628 KiB
12Accepted29ms23840 KiB
13Accepted81ms35476 KiB
14Accepted208ms50032 KiB
15Accepted312ms63944 KiB
subtask415/15
16Accepted24ms21008 KiB
17Accepted25ms21240 KiB
18Accepted25ms22008 KiB
19Accepted56ms24564 KiB
20Accepted96ms27288 KiB
subtask515/15
21Accepted17ms20984 KiB
22Accepted18ms20708 KiB
23Accepted24ms21196 KiB
24Accepted23ms21008 KiB
25Accepted21ms20836 KiB
26Accepted18ms20840 KiB
subtask640/40
27Accepted18ms20708 KiB
28Accepted100ms27280 KiB
29Accepted23ms20964 KiB
30Accepted19ms20968 KiB
31Accepted23ms22632 KiB
32Accepted28ms21604 KiB
33Accepted54ms24288 KiB
34Accepted82ms27276 KiB
35Accepted18ms21288 KiB
36Accepted25ms21420 KiB
37Accepted28ms22628 KiB
38Accepted29ms23840 KiB
39Accepted81ms35476 KiB
40Accepted208ms50032 KiB
41Accepted312ms63944 KiB
42Accepted24ms21008 KiB
43Accepted25ms21240 KiB
44Accepted25ms22008 KiB
45Accepted56ms24564 KiB
46Accepted96ms27288 KiB
47Accepted17ms20984 KiB
48Accepted18ms20708 KiB
49Accepted24ms21196 KiB
50Accepted23ms21008 KiB
51Accepted21ms20836 KiB
52Accepted18ms20840 KiB
53Accepted233ms65592 KiB
54Accepted19ms20964 KiB
55Accepted27ms22628 KiB
56Accepted27ms21860 KiB
57Accepted67ms24544 KiB
58Accepted68ms24844 KiB
59Accepted307ms67964 KiB
60Accepted159ms27904 KiB
61Accepted123ms28432 KiB
62Accepted165ms28636 KiB