Submission #926309

#TimeUsernameProblemLanguageResultExecution timeMemory
926309sheldonDuathlon (APIO18_duathlon)C++14
100 / 100
74 ms28488 KiB
#include <bits/stdc++.h>

using namespace std;

const int nax = 1e5 + 5;

vector<int> edges[nax], bcc[nax * 2];
int low[nax], tin[nax], sz[nax * 2]; 
bool visited[nax];
vector<int> st;

int timer = 0, BCC = 0, n;
int cnt = 0;
void dfsBCC (int u, int p) {
    visited[u] = 1;
    ++cnt;
    low[u] = tin[u] = ++timer;
    st.push_back(u);
    for (int v : edges[u]) {
        if (v != p) {
            if (visited[v]) {
                low[u] = min(low[u], tin[v]);
            } else {
                dfsBCC (v, u);
                low[u] = min(low[u], low[v]);
                if (low[v] >= tin[u]) {
                    ++BCC;
                    bcc[u].push_back(n + BCC);
                    do {
                        bcc[n + BCC].push_back(st.back());
                        st.pop_back();
                    } while (bcc[n + BCC].back() != v);
                }
            }
        }
    }
}
long long ans = 0;
void dfs (int u) {
    sz[u] = (u <= n);
    for (int v : bcc[u]) {
        dfs (v);
        sz[u] += sz[v];
        if (v <= n) {
            ans -= 1LL * sz[v] * (sz[v] - 1);
        }
    }
    if (u > n) {
        long long count = 0;
        for (int v : bcc[u]) {
            count += 1LL * sz[v] * (sz[v] - 1);
        }
        for (int v : bcc[u]) {
            long long have = count - 1LL * sz[v] * (sz[v] - 1);
            ans -= have;
            int up = cnt - sz[u];
            ans -= 1LL * up * (up - 1);
        }

    }
}

void solve() {
    int  m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }

    for (int i = 1; i <= n; ++i) {
        if (!visited[i]) {
            cnt = 0;
            dfsBCC (i, 0);
            ans += 1LL * cnt * (cnt - 1) * (cnt - 2);
            dfs (i);
        }
    }
   
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}
#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...