#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5+10;
int n;
int in[maxn], low[maxn], id[maxn], tt, cc;
bool is[maxn];
int sz[maxn], sub[maxn];
int comp_tree[maxn], sz_comp[maxn];
bool solved[maxn];
vector<int> grafo[maxn], tree[maxn], stk;
vector<vector<int>> comp;
void dfs(int u, int p)
{
in[u] = low[u] = ++tt;
stk.push_back(u);
for (auto v: grafo[u])
{
if (v == p) continue;
if (in[v]) low[u] = min(low[u], in[v]);
else
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= in[u])
{
is[u] = (in[u] > 1 || in[v] > 2);
comp.push_back({u});
while (stk.back() != u)
{
comp.back().push_back(stk.back());
stk.pop_back();
}
}
}
}
}
void build(void)
{
for (int i = 1; i <= n; i++)
{
if (is[i])
{
id[i] = ++cc;
sz[cc]++;
}
}
for (auto &C: comp)
{
cc++;
for (auto u: C)
{
if (!is[u])
{
id[u] = cc;
sz[cc]++;
}
else
{
tree[id[u]].push_back(cc);
tree[cc].push_back(id[u]);
}
}
}
}
ll ans;
ll choose2(int n)
{
return 1ll*n*(n-1)/2ll;
}
ll choose3(int n)
{
return 1ll*n*(n-1)*(n-2)/6ll;
}
void calc(int u, int p, int cc)
{
comp_tree[u] = cc;
sz_comp[cc] += sz[u];
sub[u] = sz[u];
for (auto v: tree[u])
{
if (v == p) continue;
calc(v, u, cc);
sub[u] += sub[v];
}
}
void solve(int u, int p)
{
solved[comp_tree[u]] = 1;
ans += 2ll*choose2(sz[p])*sz[u];
if (sz[u] >= 3) ans += 6ll*choose3(sz[u]);
if (sz[u] >= 2) ans += 4ll*choose2(sz[u])*(sub[u]-sz[u] + sz_comp[comp_tree[u]]-sub[u]);
ans += 2ll*sz[u]*(sz_comp[comp_tree[u]]-sub[u])*(sub[u]-sz[u]);
int soma = 0;
for (auto v: tree[u])
{
if (v == p) continue;
ans += 2ll*sz[u]*soma*sub[v];
soma += sub[v];
ans += 2ll*choose2(sz[v])*sz[u];
solve(v, u);
}
}
int main(void)
{
int m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
grafo[u].push_back(v);
grafo[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!in[i])
dfs(i, 0);
build();
int cc_tree = 0;
for (int i = 1; i <= 2*n; i++)
if (!comp_tree[i])
calc(i, 0, ++cc_tree);
for (int i = 1; i <= 2*n; i++)
if (!solved[comp_tree[i]])
solve(i, 0);
printf("%lld\n", ans);
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:144:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
144 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:149:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
149 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
9 ms |
14444 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
9 ms |
14444 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
33136 KB |
Output is correct |
2 |
Correct |
104 ms |
33292 KB |
Output is correct |
3 |
Correct |
126 ms |
33952 KB |
Output is correct |
4 |
Correct |
110 ms |
32932 KB |
Output is correct |
5 |
Correct |
95 ms |
29604 KB |
Output is correct |
6 |
Correct |
121 ms |
33056 KB |
Output is correct |
7 |
Correct |
112 ms |
31392 KB |
Output is correct |
8 |
Correct |
131 ms |
31904 KB |
Output is correct |
9 |
Correct |
118 ms |
29984 KB |
Output is correct |
10 |
Correct |
102 ms |
29600 KB |
Output is correct |
11 |
Correct |
93 ms |
27296 KB |
Output is correct |
12 |
Correct |
90 ms |
26912 KB |
Output is correct |
13 |
Correct |
77 ms |
26912 KB |
Output is correct |
14 |
Correct |
80 ms |
26672 KB |
Output is correct |
15 |
Correct |
61 ms |
25760 KB |
Output is correct |
16 |
Correct |
65 ms |
25632 KB |
Output is correct |
17 |
Correct |
16 ms |
18788 KB |
Output is correct |
18 |
Correct |
16 ms |
18788 KB |
Output is correct |
19 |
Correct |
16 ms |
18788 KB |
Output is correct |
20 |
Correct |
16 ms |
18788 KB |
Output is correct |
21 |
Correct |
16 ms |
18660 KB |
Output is correct |
22 |
Correct |
16 ms |
18660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14700 KB |
Output is correct |
2 |
Correct |
9 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14700 KB |
Output is correct |
4 |
Correct |
10 ms |
14700 KB |
Output is correct |
5 |
Correct |
12 ms |
14700 KB |
Output is correct |
6 |
Correct |
11 ms |
14700 KB |
Output is correct |
7 |
Correct |
10 ms |
14700 KB |
Output is correct |
8 |
Correct |
10 ms |
14700 KB |
Output is correct |
9 |
Correct |
10 ms |
14700 KB |
Output is correct |
10 |
Correct |
10 ms |
14572 KB |
Output is correct |
11 |
Correct |
10 ms |
14700 KB |
Output is correct |
12 |
Correct |
10 ms |
14572 KB |
Output is correct |
13 |
Correct |
10 ms |
14572 KB |
Output is correct |
14 |
Correct |
10 ms |
14572 KB |
Output is correct |
15 |
Correct |
9 ms |
14572 KB |
Output is correct |
16 |
Correct |
12 ms |
14572 KB |
Output is correct |
17 |
Correct |
11 ms |
14572 KB |
Output is correct |
18 |
Correct |
10 ms |
14572 KB |
Output is correct |
19 |
Correct |
9 ms |
14700 KB |
Output is correct |
20 |
Correct |
9 ms |
14572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
31644 KB |
Output is correct |
2 |
Correct |
128 ms |
31772 KB |
Output is correct |
3 |
Correct |
132 ms |
31772 KB |
Output is correct |
4 |
Correct |
137 ms |
31772 KB |
Output is correct |
5 |
Correct |
139 ms |
31660 KB |
Output is correct |
6 |
Correct |
143 ms |
41376 KB |
Output is correct |
7 |
Correct |
141 ms |
38044 KB |
Output is correct |
8 |
Correct |
135 ms |
37404 KB |
Output is correct |
9 |
Correct |
135 ms |
35612 KB |
Output is correct |
10 |
Correct |
140 ms |
31644 KB |
Output is correct |
11 |
Correct |
126 ms |
31732 KB |
Output is correct |
12 |
Correct |
140 ms |
31772 KB |
Output is correct |
13 |
Correct |
122 ms |
31644 KB |
Output is correct |
14 |
Correct |
120 ms |
30876 KB |
Output is correct |
15 |
Correct |
102 ms |
29852 KB |
Output is correct |
16 |
Correct |
73 ms |
26400 KB |
Output is correct |
17 |
Correct |
79 ms |
30680 KB |
Output is correct |
18 |
Correct |
76 ms |
30612 KB |
Output is correct |
19 |
Correct |
78 ms |
30736 KB |
Output is correct |
20 |
Correct |
83 ms |
30620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
10 ms |
14572 KB |
Output is correct |
3 |
Incorrect |
10 ms |
14572 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
31772 KB |
Output is correct |
2 |
Correct |
124 ms |
31900 KB |
Output is correct |
3 |
Incorrect |
138 ms |
30364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
9 ms |
14444 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14572 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14444 KB |
Output is correct |
6 |
Correct |
9 ms |
14444 KB |
Output is correct |
7 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |