#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 1e5 + 5;
int num;
int a[N];
int id[N];
int sz[N];
bool c[N];
long long ans;
stack<int> stk;
int vis[N], low[N];
bool is_cutpoint[N];
vector<int> g[N], t[N];
vector<vector<int>> bcc;
void dfs(int v, int p) {
vis[v] = low[v] = ++num;
stk.push(v);
for (int u : g[v]) {
if (u == p) {
continue;
}
if (vis[u]) {
low[v] = min(low[v], vis[u]);
continue;
}
dfs(u, v);
low[v] = min(low[v], low[u]);
if (low[u] >= vis[v]) {
is_cutpoint[v] = true;
bcc.push_back({});
bcc.back().push_back(v);
while (bcc.back().back() != u) {
bcc.back().push_back(stk.top());
stk.pop();
}
}
}
}
void dfs2(int v, int p) {
vis[v] = true;
sz[v] = a[v];
for (int u : t[v]) {
if (u == p) continue;
dfs2(u, v);
sz[v] += sz[u];
}
}
void dfs3(int v, int p, int n) {
for (int u : t[v]) {
if (u == p) continue;
dfs3(u, v, n);
if (c[v]) {
ans -= (long long) sz[u] * (n - sz[u]) * (n - sz[u] - 1);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
dfs(i, i);
if (g[i].size() <= 1) {
is_cutpoint[i] = false;
}
}
}
int idd = 0;
for (int i = 1; i <= n; ++i) {
if (is_cutpoint[i]) {
c[idd] = true;
a[idd] = 1;
id[i] = idd++;
}
}
for (int i = 0; i < (int) bcc.size(); ++i) {
vector<int>& comp = bcc[i];
int cur = idd++;
for (int v : comp) {
if (is_cutpoint[v]) {
t[id[v]].push_back(cur);
t[cur].push_back(id[v]);
} else {
id[v] = cur;
a[cur]++;
}
}
}
memset(vis, 0, sizeof vis);
for (int i = 0; i < idd; ++i) {
if (!vis[i]) {
dfs2(i, i);
dfs3(i, i, sz[i]);
ans += (long long) sz[i] * (sz[i] - 1) * (sz[i] - 2);
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6792 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6792 KB |
Output is correct |
6 |
Correct |
1 ms |
6788 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6784 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6792 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6792 KB |
Output is correct |
6 |
Correct |
1 ms |
6788 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6784 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
23004 KB |
Output is correct |
2 |
Correct |
42 ms |
23164 KB |
Output is correct |
3 |
Runtime error |
58 ms |
44944 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
61 ms |
46712 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
56 ms |
45996 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6792 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6792 KB |
Output is correct |
6 |
Correct |
1 ms |
6788 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6784 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6792 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6792 KB |
Output is correct |
6 |
Correct |
1 ms |
6788 KB |
Output is correct |
7 |
Incorrect |
1 ms |
6784 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |