110022024-06-04 11:13:24gortomiÚtvonalakcpp17Runtime error 15/100146ms28944 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(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(n + 1);
    p.resize(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
1Accepted3ms632 KiB
2Accepted101ms25780 KiB
subtask20/15
3Accepted3ms612 KiB
4Accepted4ms868 KiB
5Runtime error4ms1604 KiB
6Accepted10ms2916 KiB
7Accepted43ms13152 KiB
8Accepted93ms25468 KiB
subtask30/15
9Runtime error3ms880 KiB
10Runtime error4ms916 KiB
11Runtime error6ms1656 KiB
12Runtime error8ms2224 KiB
13Runtime error32ms8728 KiB
14Runtime error76ms16900 KiB
15Runtime error78ms24772 KiB
subtask415/15
16Accepted4ms1036 KiB
17Accepted6ms1508 KiB
18Accepted10ms3200 KiB
19Accepted46ms12956 KiB
20Accepted100ms25696 KiB
subtask50/15
21Accepted3ms632 KiB
22Accepted3ms540 KiB
23Runtime error3ms1036 KiB
24Accepted3ms784 KiB
25Accepted3ms620 KiB
26Accepted3ms760 KiB
subtask60/40
27Accepted2ms356 KiB
28Accepted97ms23712 KiB
29Accepted3ms612 KiB
30Accepted4ms868 KiB
31Runtime error4ms1604 KiB
32Accepted10ms2916 KiB
33Accepted43ms13152 KiB
34Accepted93ms25468 KiB
35Runtime error3ms880 KiB
36Runtime error4ms916 KiB
37Runtime error6ms1656 KiB
38Runtime error8ms2224 KiB
39Runtime error32ms8728 KiB
40Runtime error76ms16900 KiB
41Runtime error78ms24772 KiB
42Accepted4ms1036 KiB
43Accepted6ms1508 KiB
44Accepted10ms3200 KiB
45Accepted46ms12956 KiB
46Accepted100ms25696 KiB
47Accepted3ms632 KiB
48Accepted3ms540 KiB
49Runtime error3ms1036 KiB
50Accepted3ms784 KiB
51Accepted3ms620 KiB
52Accepted3ms760 KiB
53Runtime error76ms26736 KiB
54Accepted4ms996 KiB
55Runtime error4ms1636 KiB
56Accepted14ms3172 KiB
57Accepted64ms13876 KiB
58Accepted57ms15712 KiB
59Runtime error82ms28944 KiB
60Accepted146ms26972 KiB
61Accepted136ms27364 KiB
62Accepted135ms27436 KiB