#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]) {
// for (int v : comp[cur]) {
// if (u != v) {
// ans += 2LL * (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:108: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:111: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 |
Incorrect |
8 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
165 ms |
16360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16360 KB |
Output is correct |
2 |
Correct |
8 ms |
16360 KB |
Output is correct |
3 |
Correct |
7 ms |
16360 KB |
Output is correct |
4 |
Correct |
7 ms |
16360 KB |
Output is correct |
5 |
Correct |
8 ms |
16360 KB |
Output is correct |
6 |
Correct |
9 ms |
16360 KB |
Output is correct |
7 |
Correct |
9 ms |
16360 KB |
Output is correct |
8 |
Correct |
6 ms |
16360 KB |
Output is correct |
9 |
Correct |
7 ms |
16360 KB |
Output is correct |
10 |
Correct |
8 ms |
16360 KB |
Output is correct |
11 |
Correct |
10 ms |
16360 KB |
Output is correct |
12 |
Correct |
8 ms |
16360 KB |
Output is correct |
13 |
Correct |
8 ms |
16360 KB |
Output is correct |
14 |
Correct |
8 ms |
16360 KB |
Output is correct |
15 |
Correct |
8 ms |
16360 KB |
Output is correct |
16 |
Correct |
7 ms |
16360 KB |
Output is correct |
17 |
Correct |
7 ms |
16360 KB |
Output is correct |
18 |
Correct |
8 ms |
16360 KB |
Output is correct |
19 |
Correct |
7 ms |
16360 KB |
Output is correct |
20 |
Correct |
7 ms |
16360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
293 ms |
16360 KB |
Output is correct |
2 |
Correct |
223 ms |
16360 KB |
Output is correct |
3 |
Correct |
181 ms |
16360 KB |
Output is correct |
4 |
Correct |
196 ms |
16360 KB |
Output is correct |
5 |
Correct |
207 ms |
16360 KB |
Output is correct |
6 |
Correct |
222 ms |
20272 KB |
Output is correct |
7 |
Correct |
214 ms |
20272 KB |
Output is correct |
8 |
Correct |
217 ms |
20272 KB |
Output is correct |
9 |
Correct |
235 ms |
20272 KB |
Output is correct |
10 |
Correct |
268 ms |
20272 KB |
Output is correct |
11 |
Correct |
255 ms |
20272 KB |
Output is correct |
12 |
Correct |
190 ms |
20272 KB |
Output is correct |
13 |
Correct |
229 ms |
20272 KB |
Output is correct |
14 |
Correct |
167 ms |
20272 KB |
Output is correct |
15 |
Correct |
248 ms |
20272 KB |
Output is correct |
16 |
Correct |
158 ms |
20272 KB |
Output is correct |
17 |
Correct |
162 ms |
20272 KB |
Output is correct |
18 |
Correct |
181 ms |
20272 KB |
Output is correct |
19 |
Correct |
134 ms |
20272 KB |
Output is correct |
20 |
Correct |
170 ms |
20272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
20272 KB |
Output is correct |
2 |
Correct |
11 ms |
20272 KB |
Output is correct |
3 |
Incorrect |
9 ms |
20272 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
20272 KB |
Output is correct |
2 |
Correct |
259 ms |
20272 KB |
Output is correct |
3 |
Incorrect |
211 ms |
20272 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |