Submission #850004

#TimeUsernameProblemLanguageResultExecution timeMemory
850004nononoDuathlon (APIO18_duathlon)C++17
100 / 100
69 ms28244 KiB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 1e5 + 10;

int n, m;
vector<vector<int>> g(mxN), bcc_graph(mxN << 1);

int N = 0;
int num[mxN], low[mxN], timer = 0;
stack<int> st;

int siz[mxN << 1];
int bccs = 1;
long long sum = 0;

void tanjan(int u, int par) {
    N ++ ;
    num[u] = low[u] = ++ timer;
    st.push(u);
    
    for(int v : g[u]) if(v != par) {
        if(num[v]) low[u] = min(low[u], num[v]);
        else {
            tanjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] >= num[u]) {
                bcc_graph[u].push_back(n + bccs);
                
                while(bcc_graph[n + bccs].size() == 0 || bcc_graph[n + bccs].back() != v) {
                    bcc_graph[n + bccs].push_back(st.top());
                    st.pop();
                }  
                
                bccs ++ ;
            }
        }
    }
}

void dfs(int u) {
    siz[u] = (u <= n);
    
    for(int v : bcc_graph[u]) {
        dfs(v);
        
        siz[u] += siz[v];
        if(u > n) sum -= 1LL * bcc_graph[u].size() * siz[v] * (siz[v] - 1);
    }
    
    if(u > n) sum -= 1LL * bcc_graph[u].size() * (N - siz[u]) * (N - siz[u] - 1);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> m;
    
    for(int i = 1; i <= m; i ++) {
        int u, v;
        cin >> u >> v;
        
        g[u].push_back(v);
        g[v].push_back(u);
    }
    
    for(int i = 1; i <= n; i ++) {
        if(num[i]) continue ;
        N = 0;
        
        tanjan(i, 0);
        sum += 1LL * N * (N - 1) * (N - 2);
        dfs(i);
    }
    
    cout << sum << "\n";
    return 0;
}
#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...