110092024-06-04 13:54:07taxiradioÚtvonalakcpp17Time limit exceeded 0/1001.1s54100 KiB
#include<iostream>
#include<vector>
#include<stack>
#include<array>

using namespace std;

vector<vector<int>> g , g2;
vector<int> e , l , ti , z;
stack<int> s;
vector<bool> cp , vis , h;
vector<vector<int>> b;
int t = 0 , v = 0;

void dfs(int p , int q){
    ti[p] = t++;
    l[p] = ti[p];
    s.push(p);
    int c = 0;
    for(int x : g[p]){
        if(x == q)continue;
        if(l[x] == -1){
            c++;
            dfs(x , p);
            if(ti[p] <= l[x]){
                b.push_back({});
                b.back().push_back(p);
                g2.push_back({});
                
                if(p != 0)cp[p] = true;
                e[p] = b.size()-1;
                while(s.top() != p){
                    b.back().push_back(s.top());
                    e[s.top()] = b.size()-1;
                    if(s.top() == x){
                        s.pop();
                        break;
                    }
                    s.pop();
                }
                z.push_back(b.back().size()-2);
            }
            l[p] = min(l[p] , l[x]);
        }else{
            l[p] = min(l[p] , ti[x]);
        }
    }
    if(p == 0 && c > 2)cp[0] = true;
}

vector<array<int , 22>> bl;
vector<int> o , f;

void blH(int p , int q){
    //cout << p << " " << q << endl;
    bl[p][0] = q;
    for(int i = 1 ; i < 22; i++){
        if(bl[p][i-1] != -1)bl[p][i] = bl[bl[p][i-1]][i-1];else bl[p][i] = -1;
    }
    for(int x : g2[p]){
        if(x == q)continue;
        o[x] = o[p]+z[x];
        f[x] = f[p]+1;
        blH(x , p);
    }
}

void qu(int a , int b){
    int w = cp[a-1] + cp[b-1];
    a = e[a-1];
    b = e[b-1];
    int ans = o[a]+o[b];
    if(f[a] < f[b])swap(a , b);
    int r = f[a]-f[b];
    for(int i = 0; i < 22; i++){
        if(r&(1<<i)){
            a = bl[a][i];
        }
    }
    if(a != b){
        for(int i = 21; i >= 0; i++){
            if(bl[a][i] != bl[b][i]){
                a = bl[a][i];
                b = bl[b][i];
            }
        }
        a = bl[a][0];
    }
    cout << (ans-2*o[a]+z[a]-w) << "\n";
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n , m , k;cin >> n >> m >> k;
    g.resize(n);
    e.resize(n , -1);
    ti.resize(n);
    l.resize(n , -1);
    cp.resize(n , false);
    vis.resize(n , false);
    h.resize(n , false);
    for(int i = 0; i < m; i++){
        int a , b;cin >> a >> b;
        g[a-1].push_back(b-1);
        g[b-1].push_back(a-1);
    }
    dfs(0 , -1);
    int u = b.size();
    for(int i  = 0 ; i < n; i++){
        if(cp[i]){
            e[i] = u++;
            g2.push_back({});
            z.push_back(1);
        }
    }
    for(int i = 0; i < b.size(); i++){
        for(int x:b[i]){
            if(cp[x]){
                g2[i].push_back(e[x]);
                g2[e[x]].push_back(i);
            }
        }
    }
    bl.resize(u);
    o.resize(u , 0);
    f.resize(u , 0);
    blH(0 , -1);
    for(int i = 0; i < k; i++){
        int a , b;cin >> a >> b;
        qu(a , b);
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms360 KiB
2Time limit exceeded1.097s8708 KiB
subtask20/15
3Accepted3ms748 KiB
4Accepted4ms884 KiB
5Time limit exceeded1.088s2464 KiB
6Accepted8ms1508 KiB
7Accepted35ms4584 KiB
8Accepted76ms8972 KiB
subtask30/15
9Time limit exceeded1.09s772 KiB
10Time limit exceeded1.093s1308 KiB
11Time limit exceeded1.088s2584 KiB
12Time limit exceeded1.09s3948 KiB
13Time limit exceeded1.097s18008 KiB
14Time limit exceeded1.098s35656 KiB
15Time limit exceeded1.1s50236 KiB
subtask40/15
16Time limit exceeded1.097s624 KiB
17Time limit exceeded1.095s764 KiB
18Time limit exceeded1.093s1380 KiB
19Time limit exceeded1.097s4580 KiB
20Time limit exceeded1.097s8800 KiB
subtask50/15
21Accepted3ms632 KiB
22Accepted3ms412 KiB
23Time limit exceeded1.088s1032 KiB
24Time limit exceeded1.092s508 KiB
25Time limit exceeded1.095s504 KiB
26Time limit exceeded1.097s484 KiB
subtask60/40
27Accepted3ms356 KiB
28Time limit exceeded1.09s8732 KiB
29Accepted3ms748 KiB
30Accepted4ms884 KiB
31Time limit exceeded1.088s2464 KiB
32Accepted8ms1508 KiB
33Accepted35ms4584 KiB
34Accepted76ms8972 KiB
35Time limit exceeded1.09s772 KiB
36Time limit exceeded1.093s1308 KiB
37Time limit exceeded1.088s2584 KiB
38Time limit exceeded1.09s3948 KiB
39Time limit exceeded1.097s18008 KiB
40Time limit exceeded1.098s35656 KiB
41Time limit exceeded1.1s50236 KiB
42Time limit exceeded1.097s624 KiB
43Time limit exceeded1.095s764 KiB
44Time limit exceeded1.093s1380 KiB
45Time limit exceeded1.097s4580 KiB
46Time limit exceeded1.097s8800 KiB
47Accepted3ms632 KiB
48Accepted3ms412 KiB
49Time limit exceeded1.088s1032 KiB
50Time limit exceeded1.092s508 KiB
51Time limit exceeded1.095s504 KiB
52Time limit exceeded1.097s484 KiB
53Time limit exceeded1.09s52184 KiB
54Time limit exceeded1.088s896 KiB
55Time limit exceeded1.098s2536 KiB
56Time limit exceeded1.095s1432 KiB
57Time limit exceeded1.093s4624 KiB
58Time limit exceeded1.092s5604 KiB
59Time limit exceeded1.095s54100 KiB
60Accepted108ms9200 KiB
61Time limit exceeded1.095s9180 KiB
62Time limit exceeded1.095s9624 KiB