제출 #849983

#제출 시각아이디문제언어결과실행 시간메모리
849983nononoDuathlon (APIO18_duathlon)C++14
0 / 100
42 ms23100 KiB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 1e5 + 10;

int n, m;
vector<vector<int>> g(mxN), bcc_graph(mxN);
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) {
    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(!st.empty() && st.top() != u) {
                    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 <= 2 * n; i ++) siz[i] = -1;
    
    for(int i = 1; i <= n; i ++) {
        if(num[i]) continue ;
        
        int prev_timer = timer;
        tanjan(i, 0);
        
        int x = timer - prev_timer;
        sum += x * (x - 1) * (x - 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...