Submission #1118726

#TimeUsernameProblemLanguageResultExecution timeMemory
1118726floDuathlon (APIO18_duathlon)C++14
100 / 100
111 ms30532 KiB
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAX_N = 2e5 + 7;

int time_in[MAX_N], low[MAX_N], size[MAX_N];
int timer;
vector<int> adj[MAX_N], new_graph[MAX_N];
int n, m, comp_count, component_size;
stack<int> stk;
ll result = 0;

void explore(int u, int parent) {
    time_in[u] = low[u] = ++timer;
    stk.push(u);
    component_size++;
    
    for (int v : adj[u]) {
        if (v == parent) continue;
        
        if (time_in[v] == 0) {
            explore(v, u);
            low[u] = min(low[u], low[v]);
            
            if (low[v] >= time_in[u]) {
                comp_count++;
                new_graph[u].push_back(n + comp_count);
                
                while (new_graph[n + comp_count].empty() || new_graph[n + comp_count].back() != v) {
                    new_graph[n + comp_count].push_back(stk.top());
                    stk.pop();
                }
            }
        } else {
            low[u] = min(low[u], time_in[v]);
        }
    }
}

void calculate_size(int u) {
    size[u] = (u <= n);
    
    for (int v : new_graph[u]) {
        calculate_size(v);
        size[u] += size[v];
        
        if (u > n) {
            result -= (new_graph[u].size() * 1LL * (size[v] - 1) * size[v]);
        }
    }
    
    if (u > n) {
        result -= new_graph[u].size() * 1LL * (component_size - size[u]) * (component_size - size[u] - 1);
    }
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
    cin >> n >> m;
    
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    for (int i = 1; i <= n; i++) {
        if (time_in[i] == 0) {
            component_size = 0;
            explore(i, i);
            result += component_size * 1LL * (component_size - 1) * (component_size - 2);
            calculate_size(i);
        }
    }
    
    cout << result;
}
#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...