#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1e5;
std::vector<int> e[N];
int n, m;
std::vector<bool> vis;
bool cycle;
int sz;
void dfs(int u, int p) {
sz++;
vis[u] = true;
for (auto v : e[u]) {
if (v == p)continue;
if (vis[v]) cycle = true;
else dfs(v, u);
}
}
i64 ans;
int calc(int u, int p) {
int cnt = 0;
for (auto v : e[u]) {
if (v != p) {
cnt += calc(v, u);
}
}
ans += i64(cnt) * (sz - cnt - 1) * 2ll;
return cnt + 1;
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
vis.assign(n, false);
for (int i = 0; i < n; i++) {
if (!vis[i]) {
sz = 0;
cycle = false;
dfs(i, i);
if (cycle) {
ans += i64(sz) * i64(sz - 1) * i64(sz - 2) / 3ll * 2ll;
} else {
calc(i, i);
}
}
}
std::cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
10476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
84 ms |
5800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
5868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |