#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
vector<int> adj[N], g[N << 1], stk;
int n, m, st[N], low[N], timer = 0, k, we[N << 1], sz[N << 1], ans = 0, p[N];
bool vis[N << 1];
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
p[v] = u;
}
void dfs(int u) {
low[u] = st[u] = ++timer;
stk.push_back(u);
for (auto& 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[k].push_back(x);
g[x].push_back(k);
merge(x, k);
we[k]++;
}
g[k].push_back(u);
g[u].push_back(k);
merge(u, k);
we[k]++;
}
}
low[u] = min(low[u], st[v]);
}
}
void comp(int u, int p) {
sz[u] = (u <= n);
vis[u] = true;
int ch = 0;
for (int v : g[u]) {
if (v == p) continue;
++ch;
comp(v, u);
sz[u] += sz[v];
// cout << "hi " << u << " " << sz[u] << '\n';
}
if (!ch) ans--;
}
void Dfs(int u, int p) {
int sum = 0;
vis[u] = true;
for (int v : g[u]) {
if (v == p) continue;
Dfs(v, u);
sum += sz[v];
}
for (int v : g[u]) if (v != p) ans += we[u] * sz[v] * (sum - sz[v]);
ans += we[u] * sum * (sz[find(u)] - sum);
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
if (n <= 2) {
cout << "0\n";
return 0;
}
k = n;
for (int i = 1; i <= n; i++) we[i] = -1;
for (int i = 1; i <= 2 * n; i++) p[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
merge(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) if (!st[i]) dfs(i);
for (int i = 1; i <= k; i++) if (!vis[i]) comp(i, -1);
for (int i = 1; i <= k; i++) vis[i] = false;
for (int i = 1; i <= k; i++) if (!vis[i]) ans--, Dfs(i, -1);
// for (int i = 1; i <= n; i++) cout << sz[find(i)] << '\n';
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
30140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
104 ms |
23608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
23556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |