This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100001;
int n, m, st[N], low[N], k, ans = 0, tot = 0, sz[N], dp[N], timer = 0, siz[N << 1], f[N << 1], sizes[N << 1];
bool vis[N << 1];
vector<int> adj[N], g[N << 1], stk;
int find(int u) {
return f[u] == u ? u : f[u] = find(f[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (siz[u] < siz[v]) swap(u, v);
siz[u] += siz[v];
f[v] = u;
}
void dfs(int u) {
st[u] = low[u] = ++timer;
stk.push_back(u);
for (int v : adj[u]) {
if (!st[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
if (low[v] == st[u]) {
++k;
for (int x = -1; x != v; stk.pop_back()) {
x = stk.back();
g[x].push_back(k);
merge(x, k);
g[k].push_back(x);
// cout << x << " --> " << k << '\n';
}
g[u].push_back(k);
g[k].push_back(u);
merge(u, k);
// cout << u << " --> " << k << '\n';
}
}
low[u] = min(low[u], st[v]);
}
}
void compute(int u, int p) {
vis[u] = 1;
int sum = 0, sdp = 0;
if (u <= n) sz[u] = 1;
for (int v : g[u]) {
if (v == p) continue;
compute(v, u);
sz[u] += sz[v];
if (u <= n) ans += dp[v] * 4;
dp[u] += dp[v];
sdp += dp[v];
sum += sz[v];
}
if (u <= n) {
dp[u] += (sz[u] - 1);
}
ans += (sum * sum) * (siz[find(u)] - sz[u] - 1);
for (int v : g[u]) {
if (v == p) continue;
// ans += (sdp - dp[v]) * (sz[v]) * 2;
ans -= sz[v] * sz[v] * (siz[find(u)] - sz[u] - 1);
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) f[i] = i, siz[i] = 1;
for (int i = n + 1; i <= 2 * n; i++) f[i] = i;
k = n;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
merge(u, v);
}
for (int i = 1; i <= 2 * n; i++) sizes[find(i)] = 1;
int tot = 0;
for (int i = 1; i <= 2 * n; i++) {
if (!sizes[i]) continue;
tot += siz[i] * (siz[i] - 1) * (siz[i] - 2);
}
for (int i = 1; i <= n; i++) if (!st[i]) dfs(i);
for (int i = 1; i <= n; i++) if (!vis[i]) compute(i, -1);
cout << tot - ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |