Submission #1314271

#TimeUsernameProblemLanguageResultExecution timeMemory
1314271888313666Duathlon (APIO18_duathlon)C++20
0 / 100
1096 ms26808 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _ <<' '<<
#define print(x) cout<<#x<<": "<<(x)<<'\n'

ll n,m,c=-1,g,b,ans=0;
vector<vector<int>> adj, bcc;
vector<int> dp, num, low;
stack<int> s;

void dfs(const int u, const int p) {
    num[u]=low[u]=++c;
    s.push(u);
    ++g;
    for (const auto v:adj[u]) {
        if (v==p) continue;
        if (num[v]==-1) {
            dfs(v, u);
            low[u]=min(low[u], low[v]);
            if (low[v]>=num[u]) {
                bcc[u].push_back(n+b);
                while (bcc[n+b].empty() || bcc[n+b].back()!=v) {
                    bcc[n+b].push_back(s.top());
                    s.pop();
                }
                ++b;
            }   
        }
        low[u]=min(low[u], num[v]);
    }
}

void dfs2(const int u) {
    dp[u]=u<n;
    for (const auto v:bcc[u]) {
        dfs2(v);
        dp[u]+=dp[v];
        if (u>=n) ans-=1LL*bcc[u].size()*dp[v]*(dp[v]-1);
    }
    if (u>=n) ans-=1LL*bcc[u].size()*(g-dp[u])*(g-1-dp[u]);
}

void tj() {
    for (int u=0; u<n; u++) if (num[u]==-1) {
        s={};
        s.push(u);
        b=0;
        g=1;
        num[u]=low[u]=++c;
        for (const auto v:adj[u]) {
            if (num[v]==-1) {
                dfs(v, u);
                bcc[u].push_back(n+b);
                while (bcc[n+b].empty() || bcc[n+b].back()!=v) {
                    bcc[n+b].push_back(s.top());
                    s.pop();
                }
                ++b;
            }
        }
        ans+=g*(g-1)*(g-2);
        dfs2(u);
    }
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    cin>>n>>m;
    adj.resize(n);
    bcc.resize(n<<1);
    dp.assign(n<<1, 0);
    for (int i=0; i<m; i++) {
        int a,b;
        cin>>a>>b;
        adj[--a].push_back(--b);
        adj[b].push_back(a);
    }
    num.resize(n, -1);
    low=num;
    tj();
    cout<<ans<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...