107492024-04-11 08:10:14szilÚtvonalakcpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva17ms42400 KiB
2Elfogadva86ms55484 KiB
subtask215/15
3Elfogadva17ms42936 KiB
4Elfogadva18ms43064 KiB
5Elfogadva21ms46428 KiB
6Elfogadva24ms44672 KiB
7Elfogadva54ms49944 KiB
8Elfogadva82ms56204 KiB
subtask315/15
9Elfogadva17ms44220 KiB
10Elfogadva19ms44620 KiB
11Elfogadva21ms46820 KiB
12Elfogadva32ms49304 KiB
13Elfogadva82ms73136 KiB
14Elfogadva155ms102452 KiB
15Elfogadva294ms128928 KiB
subtask415/15
16Elfogadva19ms44048 KiB
17Elfogadva19ms44432 KiB
18Elfogadva24ms45600 KiB
19Elfogadva50ms50720 KiB
20Elfogadva85ms56872 KiB
subtask515/15
21Elfogadva21ms44144 KiB
22Elfogadva18ms44400 KiB
23Elfogadva18ms45400 KiB
24Elfogadva17ms44748 KiB
25Elfogadva17ms44676 KiB
26Elfogadva17ms44820 KiB
subtask640/40
27Elfogadva21ms44724 KiB
28Elfogadva86ms57368 KiB
29Elfogadva17ms42936 KiB
30Elfogadva18ms43064 KiB
31Elfogadva21ms46428 KiB
32Elfogadva24ms44672 KiB
33Elfogadva54ms49944 KiB
34Elfogadva82ms56204 KiB
35Elfogadva17ms44220 KiB
36Elfogadva19ms44620 KiB
37Elfogadva21ms46820 KiB
38Elfogadva32ms49304 KiB
39Elfogadva82ms73136 KiB
40Elfogadva155ms102452 KiB
41Elfogadva294ms128928 KiB
42Elfogadva19ms44048 KiB
43Elfogadva19ms44432 KiB
44Elfogadva24ms45600 KiB
45Elfogadva50ms50720 KiB
46Elfogadva85ms56872 KiB
47Elfogadva21ms44144 KiB
48Elfogadva18ms44400 KiB
49Elfogadva18ms45400 KiB
50Elfogadva17ms44748 KiB
51Elfogadva17ms44676 KiB
52Elfogadva17ms44820 KiB
53Elfogadva273ms133612 KiB
54Elfogadva19ms45044 KiB
55Elfogadva21ms48284 KiB
56Elfogadva26ms46444 KiB
57Elfogadva64ms51464 KiB
58Elfogadva57ms52820 KiB
59Elfogadva289ms137884 KiB
60Elfogadva119ms57096 KiB
61Elfogadva123ms58600 KiB
62Elfogadva120ms59644 KiB