#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], f[N], sizes[N];
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);
g[k].push_back(x);
// cout << x << " --> " << k << '\n';
}
g[u].push_back(k);
g[k].push_back(u);
// 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;
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 <= n; i++) sizes[find(i)] = 1;
int tot = 0;
for (int i = 1; i <= 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 |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
31944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
23532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
23740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |