#include <bits/stdc++.h>
using namespace std;
#define N 200000
vector<unsigned> g[N], bccg[N];
unsigned y[N], l[N], subtree_size[N], s[N];
bitset<N> visited;
size_t n, m, t = 0, n_bcc;
unsigned build_bcc_tree(unsigned u, unsigned p = -1, unsigned i = 0)
{
y[u] = l[u] = i++;
visited[u] = 1;
s[t++] = u;
for (unsigned const &v : g[u])
{
if (!visited[v])
{
i = build_bcc_tree(v, u, i);
l[u] = min(l[u], l[v]);
if (l[v] >= y[u])
{
// Collect all nodes in the BCC of u and v. u is an articulation
// point, so leave it in the stack.
bccg[u].push_back(n_bcc);
while (s[t] != v)
bccg[n_bcc].push_back(s[--t]);
n_bcc++;
}
}
else if (v != p)
l[u] = min(l[u], y[v]);
}
return i;
}
// The BCC graph is bipartite with BCCs in one partition and original nodes in
// the other.
uint64_t get_bad_triples(unsigned u, unsigned p = -1)
{
uint64_t ans = 0;
subtree_size[u] = u < n;
for (unsigned const &v : bccg[u])
{
if (v != p)
{
ans += get_bad_triples(v, u);
subtree_size[u] += subtree_size[v];
if (v < n)
ans += bccg[u].size() * subtree_size[v] * (subtree_size[v] - 1);
}
}
if (u >= n) // Add the contribution with the pair (s, f) outside u's subtree.
ans += bccg[u].size() * (n - subtree_size[u]) * (n - subtree_size[u] - 1);
return ans;
}
int main()
{
scanf("%zu %zu", &n, &m);
n_bcc = n;
for (size_t i = 0; i < m; i++)
{
unsigned u, v;
scanf("%u %u", &u, &v);
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
unsigned long long ans = 0;
for (size_t i = 0; i < n; i++)
if (!visited[i])
{
build_bcc_tree(i);
ans -= get_bad_triples(i);
ans += subtree_size[i] * (subtree_size[i] - 1) * (subtree_size[i] - 2);
}
printf("%llu\n", ans);
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%zu %zu", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
count_triplets.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%u %u", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
79 ms |
27064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9900 KB |
Output is correct |
3 |
Correct |
5 ms |
9824 KB |
Output is correct |
4 |
Correct |
6 ms |
9940 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Incorrect |
6 ms |
9708 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
20452 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
8 ms |
9712 KB |
Output is correct |
9 |
Correct |
6 ms |
9772 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9832 KB |
Output is correct |
16 |
Incorrect |
6 ms |
9712 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
20556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |