#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200001;
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], in[N];
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, int S) {
vis[u] = 1;
int sum = 0, sdp = 0;
if (in[u]) sz[u] = 1;
for (int v : g[u]) {
if (v == p) continue;
compute(v, u, S);
sz[u] += sz[v];
if (in[u]) ans += dp[v] * 4;
dp[u] += dp[v];
sdp += dp[v];
sum += sz[v];
}
if (in[u]) {
dp[u] += (sz[u] - 1);
}
ans += (sum * sum) * (S - sz[u] - 1);
for (int v : g[u]) {
if (v == p) continue;
// ans += (sdp - dp[v]) * (sz[v]) * 2;
ans -= sz[v] * sz[v] * (S - sz[u] - 1);
}
// cout << u << ' ' << ans << '\n';
}
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);
in[u] = in[v] = 1;
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, siz[find(i)]);
cout << tot - ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21152 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21152 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
41844 KB |
Output is correct |
2 |
Correct |
50 ms |
41928 KB |
Output is correct |
3 |
Incorrect |
63 ms |
39880 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
34292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
21080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
34384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21152 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21152 KB |
Output is correct |
5 |
Correct |
3 ms |
21084 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Incorrect |
3 ms |
21084 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |