110032024-06-04 11:19:06gortomiÚtvonalakcpp17Accepted 100/100444ms62612 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g, comp, g2;
vector<int> t, l;
vector<bool> c;
stack<int> s;
int tim = 0, k = 0;
void dfs(int v, int pa)
{
    t[v] = l[v] = ++tim;
    s.push(v);
    for(auto x : g[v])
    {
        if(x == pa) continue;
        if(t[x] == 0)
        {
            dfs(x, v);
            l[v] = min(l[v], l[x]);
            if(l[x] < t[v]) continue;
            //cout << x << "\n";
            if(t[v] != 1 || t[x] != 2) c[v] = 1;
            comp.push_back({});
            comp[k].push_back(v);
            while(s.top() != x)
            {
                comp[k].push_back(s.top());
                s.pop();
            }
            comp[k].push_back(x);
            s.pop();
            k++;
        }
        else l[v] = min(l[v], t[x]);
    }
}
vector<int> pref, d;
vector<vector<int> > p;
void dfs2(int v, int pa)
{

    d[v] = d[pa] + 1;
    p[v][0] = pa;
    pref[v] = pref[pa] + (v >= k ? -1 : comp[v].size());
    for(int j = 1; j < 20; j++)
    {
        p[v][j] = p[p[v][j - 1]][j - 1];
    }
    for(auto x : g2[v])
    {
        if(x == pa) continue;
        dfs2(x, v);
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    g.resize(n + 1);
    t.resize(n + 1);
    l.resize(n + 1);
    c.resize(n + 1);
    for(int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    g2.resize(2 * n + 1);
    vector<int> ind(n + 1);
    vector<int> id(n + 1);
    int z = k;
    for(int i = 1; i <= n; i++)
    {
        if(c[i]) ind[i] = z++, id[i] = ind[i];
    }
    for(int i = 0; i < k; i++)
    {
        for(auto x : comp[i])
        {
            if(c[x])
            {
                int pos = ind[x];
                g2[pos].push_back(i);
                g2[i].push_back(pos);
            }
            else id[x] = i;
        }
    }
    pref.resize(2 * n + 1);
    d.resize(2 * n + 1);
    p.resize(2 * n + 1, vector<int>(20));
    dfs2(0, z);
    while(q--)
    {
        int x, y;
        cin >> x >> y;

        int ans = 0;
        if(!c[x]) ans--;
        if(!c[y]) ans--;

        x = id[x];
        y = id[y];
        ans += pref[x] + pref[y];
        if(d[y] < d[x]) swap(x, y);
        int diff = d[y] - d[x];
        for(int j = 0; j < 20; j++)
        {
            if((diff >> j) & 1)
            {
                y = p[y][j];
            }
        }
        for(int j = 19; j >= 0; j--)
        {
            if(p[x][j] != p[y][j])
            {
                x = p[x][j];
                y = p[y][j];
            }
        }
        if(x != y)
        {
            x = p[x][0];

        }
        ans -= pref[x];
        ans -= pref[p[x][0]];
        cout << ans << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms360 KiB
2Accepted112ms38260 KiB
subtask215/15
3Accepted4ms892 KiB
4Accepted4ms1252 KiB
5Accepted8ms2916 KiB
6Accepted12ms4232 KiB
7Accepted54ms19316 KiB
8Accepted101ms38140 KiB
subtask315/15
9Accepted4ms1004 KiB
10Accepted6ms1268 KiB
11Accepted8ms2956 KiB
12Accepted17ms5196 KiB
13Accepted96ms23904 KiB
14Accepted189ms47588 KiB
15Accepted375ms59924 KiB
subtask415/15
16Accepted4ms1272 KiB
17Accepted7ms2096 KiB
18Accepted13ms4320 KiB
19Accepted52ms19296 KiB
20Accepted122ms38264 KiB
subtask515/15
21Accepted3ms360 KiB
22Accepted3ms484 KiB
23Accepted4ms1124 KiB
24Accepted3ms612 KiB
25Accepted4ms784 KiB
26Accepted3ms888 KiB
subtask640/40
27Accepted3ms496 KiB
28Accepted130ms38324 KiB
29Accepted4ms892 KiB
30Accepted4ms1252 KiB
31Accepted8ms2916 KiB
32Accepted12ms4232 KiB
33Accepted54ms19316 KiB
34Accepted101ms38140 KiB
35Accepted4ms1004 KiB
36Accepted6ms1268 KiB
37Accepted8ms2956 KiB
38Accepted17ms5196 KiB
39Accepted96ms23904 KiB
40Accepted189ms47588 KiB
41Accepted375ms59924 KiB
42Accepted4ms1272 KiB
43Accepted7ms2096 KiB
44Accepted13ms4320 KiB
45Accepted52ms19296 KiB
46Accepted122ms38264 KiB
47Accepted3ms360 KiB
48Accepted3ms484 KiB
49Accepted4ms1124 KiB
50Accepted3ms612 KiB
51Accepted4ms784 KiB
52Accepted3ms888 KiB
53Accepted358ms59632 KiB
54Accepted4ms1124 KiB
55Accepted8ms2860 KiB
56Accepted16ms4336 KiB
57Accepted75ms19552 KiB
58Accepted71ms23304 KiB
59Accepted444ms62612 KiB
60Accepted156ms38620 KiB
61Accepted158ms39156 KiB
62Accepted172ms39176 KiB