109702024-04-23 15:23:16k_balintÚtvonalakcpp17Wrong answer 15/100328ms154420 KiB
#include <bits/stdc++.h>
using namespace std;
const int c=3e5+5;
const int ce=1e6+6;

int n,m,q,tt,K;
vector<int> adj[c];
bool volt[ce], vis[c];
int vag[c], t[c], l[c], cnt[c], dep[c];
int mp[c];
vector<vector<int>> comp;
vector<pair<int,int>> s;
vector<int> tree[c];

void dfs(int v, int p){
    t[v]=l[v]=++tt;
    int vag1=0;
    for(int x:adj[v]){
        if(x==p) continue;
        if(!t[x]){
            vag1++;
            s.push_back(make_pair(v,x));
            dfs(x,v);
            l[v]=min(l[v],l[x]);
            if(l[x]>=t[v] && p){
                if(!vag[v]) vag[v]=++K;
                comp.push_back(vector<int>());
                while(!s.empty()){
                    int a=s.back().first,b=s.back().second;
                    comp.back().push_back(a);
                    comp.back().push_back(b);
                    s.pop_back();
                    if(a==v) break;
                }
            }

            if(!p){
                comp.push_back(vector<int>());
                while(!s.empty()){
                    comp.back().push_back(s.back().first);
                    comp.back().push_back(s.back().second);
                    s.pop_back();
                }
            }
        }
        else l[v]=min(l[v],t[x]);
    }

    if(vag1>1 && !p) vag[1]=++K;
}

void build(){
    for(int i=1;i<=n;i++){
        if(vag[i]) {mp[i]=vag[i]+comp.size(); cnt[mp[i]]=-1;}
    }

    for(int i=0;i<comp.size();i++){
        vector<int> cut;
        sort(comp[i].begin(),comp[i].end());
        comp[i].resize(unique(comp[i].begin(),comp[i].end())-comp[i].begin());
        cnt[i+1]=comp[i].size();

        for(int x:comp[i])
        {
            if(!vag[x]) mp[x]=i+1;
            else {
                cut.push_back(x);
                tree[i+1].push_back(mp[x]);
                tree[mp[x]].push_back(i+1);
            }
        }
    }
}

int up[c][19];
int sum[c][19];

void dfs2(int v, int p){
    up[v][0]=p;
    sum[v][0]=cnt[v];
    for(int i=1;i<19;i++){
        int x = up[v][i-1];
        up[v][i]=up[x][i-1];
        sum[v][i]=sum[v][i-1]+sum[x][i-1];
    }

    for(int x:tree[v]){
        if(x != p){
            dep[x]=dep[v]+1;
            dfs2(x,v);
        }
    }
}

int query(int a, int b){
    int res=(vag[a]>0) + (vag[b]>0);
    a=mp[a], b=mp[b];

    if(dep[a]>dep[b]) swap(a,b);
    int k=dep[b]-dep[a];
    for(int i=18;i>=0;i--){
        if((k>>i)&1){
            res+=sum[b][i];
            b=up[b][i];
        }
    }

    if(a==b){
        res+=sum[a][0];
        return res;
    }

    for(int i=18;i>=0;i--){
        if(up[a][i] != up[b][i]){
            res+=sum[a][i]+sum[b][i];
            a=up[a][i], b=up[b][i];
        }
    }
    res+=sum[up[a][0]][0];
    return res;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>m>>q;

    for(int i=1;i<=m;i++){
        int a,b; cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1,0);
    build();
    dfs2(1,0);
    while(q--){
        int a,b; cin>>a>>b;
        cout << query(a,b)-2 << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted13ms30280 KiB
2Wrong answer103ms44936 KiB
subtask215/15
3Accepted14ms30816 KiB
4Accepted14ms31144 KiB
5Accepted21ms35784 KiB
6Accepted21ms33224 KiB
7Accepted54ms38336 KiB
8Accepted100ms45888 KiB
subtask30/15
9Wrong answer14ms32260 KiB
10Wrong answer17ms33248 KiB
11Accepted19ms36512 KiB
12Wrong answer28ms39508 KiB
13Wrong answer81ms70280 KiB
14Wrong answer177ms109004 KiB
15Accepted328ms153568 KiB
subtask40/15
16Wrong answer17ms32784 KiB
17Wrong answer17ms33364 KiB
18Wrong answer20ms34480 KiB
19Wrong answer61ms40144 KiB
20Wrong answer105ms46964 KiB
subtask50/15
21Accepted13ms32828 KiB
22Accepted14ms32740 KiB
23Accepted14ms33872 KiB
24Wrong answer13ms32804 KiB
25Wrong answer16ms32952 KiB
26Wrong answer14ms32836 KiB
subtask60/40
27Accepted13ms32596 KiB
28Wrong answer103ms47072 KiB
29Accepted14ms30816 KiB
30Accepted14ms31144 KiB
31Accepted21ms35784 KiB
32Accepted21ms33224 KiB
33Accepted54ms38336 KiB
34Accepted100ms45888 KiB
35Wrong answer14ms32260 KiB
36Wrong answer17ms33248 KiB
37Accepted19ms36512 KiB
38Wrong answer28ms39508 KiB
39Wrong answer81ms70280 KiB
40Wrong answer177ms109004 KiB
41Accepted328ms153568 KiB
42Wrong answer17ms32784 KiB
43Wrong answer17ms33364 KiB
44Wrong answer20ms34480 KiB
45Wrong answer61ms40144 KiB
46Wrong answer105ms46964 KiB
47Accepted13ms32828 KiB
48Accepted14ms32740 KiB
49Accepted14ms33872 KiB
50Wrong answer13ms32804 KiB
51Wrong answer16ms32952 KiB
52Wrong answer14ms32836 KiB
53Accepted296ms153920 KiB
54Wrong answer17ms33132 KiB
55Accepted19ms37652 KiB
56Wrong answer24ms34812 KiB
57Wrong answer71ms40204 KiB
58Wrong answer65ms41856 KiB
59Accepted277ms154420 KiB
60Accepted134ms46024 KiB
61Wrong answer136ms48248 KiB
62Wrong answer137ms49576 KiB