#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+10;
const int MAXM = 2e5+10;
int n, m;
vector<int> g[MAXN];
bool ok[MAXN];
int pre[MAXN];
int link[MAXN];
int t = 0;
int rep[MAXN];
void dfs0(int cur, int prev) {
if (ok[cur]) return;
ok[cur] = 1;
link[cur] = pre[cur] = t++;
rep[cur] = cur;
for (int nx : g[cur]) {
if (nx != prev) {
if (ok[nx]) {
link[cur] = min(link[cur], pre[nx]);
} else {
dfs0(nx, cur);
link[cur] = min(link[cur], link[nx]);
if (link[nx] <= pre[cur]) { // same component (not a bridge)
rep[nx] = rep[cur];
}
}
}
}
}
void normalize(int cur) {
if (rep[cur] != cur) {
normalize(rep[cur]);
rep[cur] = rep[rep[cur]];
}
}
vector<int> comp[MAXN];
ll sz[MAXN], sub[MAXN], subComp[MAXN];
void dfs1(int cur, int prev) {
if (ok[cur]) return;
ok[cur] = 1;
sz[rep[cur]]++;
comp[rep[cur]].push_back(cur);
for (int nx : g[cur]) {
if (nx != prev) {
dfs1(nx, cur);
if (rep[nx] != rep[cur]) {
sub[cur] += subComp[rep[nx]] + sz[rep[nx]];
}
}
}
subComp[rep[cur]] += sub[cur];
}
ll ans = 0;
void dfs2(int cur, int prev, ll sum = 0) {
if (ok[cur]) return;
ok[cur] = 1;
sub[cur] += sum;
subComp[cur] += sum;
// printf("sz %lld: %lld\n", sz[cur], sz[cur] * (sz[cur] - 1) * (sz[cur] - 2));
// ans += sz[cur] * (sz[cur] - 1) * (sz[cur] - 2);
// printf("%d, %d ~ sum %lld ~ ans %ld\n", cur, prev, sum, ans);
for (int v : comp[cur]) {
ok[v] = true;
for (int nx : g[v]) {
if (nx != prev && rep[nx] != cur) {
// printf("\t%d ~> %d %lld %lld %lld %lld\n", v, nx, subComp[cur], sz[cur], -subComp[nx], -sz[nx]);
dfs2(nx, v, subComp[cur] + sz[cur] - subComp[nx] - sz[nx]);
ans += (subComp[nx] + sz[nx]) * (sub[v] - subComp[nx] - sz[nx]);
// printf("%d -> %d: %lld * %lld\n", v, nx, subComp[nx] + sz[nx], sub[v] - subComp[nx] - sz[nx]);
} else if (nx == prev) {
ans += sum * (sub[v] - sum);
// printf("%d -> %d: %lld * %lld\n", v, nx, sum, sub[v] - sum);
}
}
}
for (int u : comp[cur]) {
ans += 2LL * sub[u] * (sz[cur] - 1LL + subComp[cur] - sub[u]);
for (int v : comp[cur]) {
if (u != v) {
ans += (sub[u] + 1LL) * (sub[v] + 1LL) * (sz[cur] - 2LL);
}
}
}
sub[cur] -= sum;
subComp[cur] -= sum;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0,a,b; i < m; i++) {
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; i++) dfs0(i, i);
for (int i = 1; i <= n; i++) normalize(i);
memset(ok, 0, sizeof(ok));
for (int i = 1; i <= n; i++) dfs1(i, i);
memset(ok, 0, sizeof(ok));
// for (int i = 1; i <= n; i++) printf("%d: rep %d sub %d\n", i, rep[i], sub[i]);
// for (int i = 1; i <= n; i++) if(i == rep[i]) printf("comp %d: sz %d, subComp %d\n", i, sz[i], subComp[i]);
for (int i = 1; i <= n; i++) dfs2(i, i);
printf("%lld\n", ans);
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
count_triplets.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5280 KB |
Output is correct |
4 |
Correct |
8 ms |
5280 KB |
Output is correct |
5 |
Correct |
6 ms |
5396 KB |
Output is correct |
6 |
Correct |
6 ms |
5396 KB |
Output is correct |
7 |
Correct |
7 ms |
5396 KB |
Output is correct |
8 |
Incorrect |
8 ms |
5396 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5280 KB |
Output is correct |
4 |
Correct |
8 ms |
5280 KB |
Output is correct |
5 |
Correct |
6 ms |
5396 KB |
Output is correct |
6 |
Correct |
6 ms |
5396 KB |
Output is correct |
7 |
Correct |
7 ms |
5396 KB |
Output is correct |
8 |
Incorrect |
8 ms |
5396 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1082 ms |
16636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16636 KB |
Output is correct |
2 |
Correct |
7 ms |
16636 KB |
Output is correct |
3 |
Correct |
8 ms |
16636 KB |
Output is correct |
4 |
Correct |
6 ms |
16636 KB |
Output is correct |
5 |
Correct |
7 ms |
16636 KB |
Output is correct |
6 |
Correct |
7 ms |
16636 KB |
Output is correct |
7 |
Correct |
7 ms |
16636 KB |
Output is correct |
8 |
Correct |
8 ms |
16636 KB |
Output is correct |
9 |
Correct |
7 ms |
16636 KB |
Output is correct |
10 |
Correct |
9 ms |
16636 KB |
Output is correct |
11 |
Correct |
8 ms |
16636 KB |
Output is correct |
12 |
Correct |
7 ms |
16636 KB |
Output is correct |
13 |
Correct |
6 ms |
16636 KB |
Output is correct |
14 |
Correct |
7 ms |
16636 KB |
Output is correct |
15 |
Correct |
8 ms |
16636 KB |
Output is correct |
16 |
Correct |
8 ms |
16636 KB |
Output is correct |
17 |
Correct |
9 ms |
16636 KB |
Output is correct |
18 |
Correct |
8 ms |
16636 KB |
Output is correct |
19 |
Correct |
9 ms |
16636 KB |
Output is correct |
20 |
Correct |
10 ms |
16636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
258 ms |
16636 KB |
Output is correct |
2 |
Correct |
236 ms |
16636 KB |
Output is correct |
3 |
Correct |
197 ms |
16636 KB |
Output is correct |
4 |
Correct |
194 ms |
16636 KB |
Output is correct |
5 |
Correct |
203 ms |
16636 KB |
Output is correct |
6 |
Correct |
320 ms |
21172 KB |
Output is correct |
7 |
Correct |
295 ms |
21172 KB |
Output is correct |
8 |
Correct |
245 ms |
21172 KB |
Output is correct |
9 |
Correct |
229 ms |
21172 KB |
Output is correct |
10 |
Correct |
185 ms |
21172 KB |
Output is correct |
11 |
Correct |
261 ms |
21172 KB |
Output is correct |
12 |
Correct |
193 ms |
21172 KB |
Output is correct |
13 |
Correct |
187 ms |
21172 KB |
Output is correct |
14 |
Correct |
202 ms |
21172 KB |
Output is correct |
15 |
Correct |
176 ms |
21172 KB |
Output is correct |
16 |
Correct |
127 ms |
21172 KB |
Output is correct |
17 |
Correct |
157 ms |
21172 KB |
Output is correct |
18 |
Correct |
155 ms |
21172 KB |
Output is correct |
19 |
Correct |
168 ms |
21172 KB |
Output is correct |
20 |
Correct |
197 ms |
21172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
21172 KB |
Output is correct |
2 |
Correct |
10 ms |
21172 KB |
Output is correct |
3 |
Correct |
9 ms |
21172 KB |
Output is correct |
4 |
Correct |
9 ms |
21172 KB |
Output is correct |
5 |
Correct |
9 ms |
21172 KB |
Output is correct |
6 |
Correct |
8 ms |
21172 KB |
Output is correct |
7 |
Correct |
9 ms |
21172 KB |
Output is correct |
8 |
Correct |
9 ms |
21172 KB |
Output is correct |
9 |
Correct |
9 ms |
21172 KB |
Output is correct |
10 |
Correct |
9 ms |
21172 KB |
Output is correct |
11 |
Correct |
9 ms |
21172 KB |
Output is correct |
12 |
Correct |
11 ms |
21172 KB |
Output is correct |
13 |
Correct |
10 ms |
21172 KB |
Output is correct |
14 |
Correct |
8 ms |
21172 KB |
Output is correct |
15 |
Correct |
8 ms |
21172 KB |
Output is correct |
16 |
Correct |
8 ms |
21172 KB |
Output is correct |
17 |
Correct |
9 ms |
21172 KB |
Output is correct |
18 |
Correct |
7 ms |
21172 KB |
Output is correct |
19 |
Correct |
8 ms |
21172 KB |
Output is correct |
20 |
Correct |
8 ms |
21172 KB |
Output is correct |
21 |
Correct |
8 ms |
21172 KB |
Output is correct |
22 |
Correct |
9 ms |
21172 KB |
Output is correct |
23 |
Correct |
7 ms |
21172 KB |
Output is correct |
24 |
Correct |
7 ms |
21172 KB |
Output is correct |
25 |
Correct |
7 ms |
21172 KB |
Output is correct |
26 |
Correct |
8 ms |
21172 KB |
Output is correct |
27 |
Correct |
7 ms |
21172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
21172 KB |
Output is correct |
2 |
Correct |
239 ms |
21172 KB |
Output is correct |
3 |
Correct |
206 ms |
21172 KB |
Output is correct |
4 |
Correct |
200 ms |
21172 KB |
Output is correct |
5 |
Correct |
185 ms |
21172 KB |
Output is correct |
6 |
Correct |
215 ms |
21172 KB |
Output is correct |
7 |
Correct |
215 ms |
21172 KB |
Output is correct |
8 |
Correct |
203 ms |
21172 KB |
Output is correct |
9 |
Correct |
201 ms |
21544 KB |
Output is correct |
10 |
Correct |
202 ms |
22888 KB |
Output is correct |
11 |
Correct |
181 ms |
24168 KB |
Output is correct |
12 |
Correct |
258 ms |
25264 KB |
Output is correct |
13 |
Correct |
350 ms |
26072 KB |
Output is correct |
14 |
Execution timed out |
1096 ms |
27344 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5280 KB |
Output is correct |
4 |
Correct |
8 ms |
5280 KB |
Output is correct |
5 |
Correct |
6 ms |
5396 KB |
Output is correct |
6 |
Correct |
6 ms |
5396 KB |
Output is correct |
7 |
Correct |
7 ms |
5396 KB |
Output is correct |
8 |
Incorrect |
8 ms |
5396 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5280 KB |
Output is correct |
4 |
Correct |
8 ms |
5280 KB |
Output is correct |
5 |
Correct |
6 ms |
5396 KB |
Output is correct |
6 |
Correct |
6 ms |
5396 KB |
Output is correct |
7 |
Correct |
7 ms |
5396 KB |
Output is correct |
8 |
Incorrect |
8 ms |
5396 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |