#include <bits/stdc++.h>
using namespace std;
struct dsu {
int nCC;
vector<int> pr, sz, rank, edges;
dsu(int n) {
nCC = n;
pr.resize(n);
sz.assign(n, 1);
edges.assign(n, 0);
rank.assign(n, 1);
iota(pr.begin(), pr.end(), 0);
}
int fnd(int u) {
if (u == pr[u]) {
return u;
}
pr[u] = fnd(pr[u]);
return pr[u];
}
bool unate(int u, int v) {
u = fnd(u); v = fnd(v);
if (u == v) {
edges[u]++;
return false;
}
--nCC;
if (rank[u] > rank[v]) {
swap(u, v);
}
pr[u] = v;
rank[v] += (rank[u] == rank[v]);
sz[v] += sz[u];
edges[v] += 1 + edges[u];
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
dsu se(n);
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u; --v;
se.unate(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> vis(n, 0);
vector<int> done(n, 0);
vector<int> stk;
function<int(int, int)> DFS = [&](int u, int p) {
if (done[u]) {
int R = 1;
while (stk.back() != u) {
stk.pop_back();
R++;
}
return R;
}
done[u] = 1;
stk.push_back(u);
for (int v : g[u]) {
if (v != p) {
int R = DFS(v, u);
if (R) {
return R;
}
}
}
stk.pop_back();
return 0;
};
long long ans = 0;
for (int i = 0; i < n; i++) {
int P = se.fnd(i);
if (!vis[P]) {
vis[P] = 1;
if (se.sz[P] > 2) {
if (se.edges[P] == se.sz[P]) {
stk.clear();
long long cycLen = DFS(P, -1);
if (se.sz[P] - cycLen > 2) {
ans += (se.sz[P] - cycLen) * 1LL * (se.sz[P] - cycLen - 1) * (se.sz[P] - cycLen - 2) / 3;
}
if (se.sz[P] - cycLen > 1) {
ans += (se.sz[P] - cycLen) * 1LL * (se.sz[P] - cycLen - 1) * (cycLen);
}
if (se.sz[P] - cycLen > 0) {
ans += (se.sz[P] - cycLen) * (cycLen - 1) * 2LL * (cycLen) - 2LL * (cycLen - 1);
}
ans += (cycLen - 1) * 1LL * (cycLen - 2) * (cycLen);
} else {
ans += se.sz[P] * 1LL * (se.sz[P] - 1) * (se.sz[P] - 2) / 3;
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
18032 KB |
Output is correct |
2 |
Correct |
78 ms |
19216 KB |
Output is correct |
3 |
Correct |
62 ms |
14376 KB |
Output is correct |
4 |
Correct |
77 ms |
16760 KB |
Output is correct |
5 |
Correct |
68 ms |
12820 KB |
Output is correct |
6 |
Correct |
59 ms |
12740 KB |
Output is correct |
7 |
Correct |
72 ms |
11448 KB |
Output is correct |
8 |
Correct |
61 ms |
12192 KB |
Output is correct |
9 |
Correct |
69 ms |
10436 KB |
Output is correct |
10 |
Correct |
60 ms |
11328 KB |
Output is correct |
11 |
Correct |
52 ms |
9156 KB |
Output is correct |
12 |
Correct |
57 ms |
8956 KB |
Output is correct |
13 |
Correct |
54 ms |
9004 KB |
Output is correct |
14 |
Correct |
69 ms |
8760 KB |
Output is correct |
15 |
Correct |
40 ms |
8264 KB |
Output is correct |
16 |
Correct |
34 ms |
8080 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
5 ms |
4916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
8120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
8128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |