#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);
}
}
}
ll prodSum = 0;
for (int u : comp[cur]) {
ans += 2LL * sub[u] * (sz[cur] - 1LL + subComp[cur] - sub[u]);
ans += 2LL * prodSum * (sub[u] + 1LL) * (sz[cur] - 2LL);
prodSum += (sub[u] + 1LL);
}
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:107: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:110:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5220 KB |
Output is correct |
3 |
Correct |
8 ms |
5220 KB |
Output is correct |
4 |
Correct |
7 ms |
5220 KB |
Output is correct |
5 |
Correct |
6 ms |
5296 KB |
Output is correct |
6 |
Correct |
8 ms |
5340 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5220 KB |
Output is correct |
3 |
Correct |
8 ms |
5220 KB |
Output is correct |
4 |
Correct |
7 ms |
5220 KB |
Output is correct |
5 |
Correct |
6 ms |
5296 KB |
Output is correct |
6 |
Correct |
8 ms |
5340 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
16536 KB |
Output is correct |
2 |
Correct |
206 ms |
16536 KB |
Output is correct |
3 |
Correct |
197 ms |
18272 KB |
Output is correct |
4 |
Correct |
236 ms |
18272 KB |
Output is correct |
5 |
Correct |
185 ms |
18272 KB |
Output is correct |
6 |
Correct |
252 ms |
18272 KB |
Output is correct |
7 |
Correct |
227 ms |
18272 KB |
Output is correct |
8 |
Correct |
234 ms |
18272 KB |
Output is correct |
9 |
Correct |
237 ms |
18272 KB |
Output is correct |
10 |
Correct |
216 ms |
18272 KB |
Output is correct |
11 |
Correct |
174 ms |
18272 KB |
Output is correct |
12 |
Correct |
190 ms |
18272 KB |
Output is correct |
13 |
Correct |
180 ms |
18272 KB |
Output is correct |
14 |
Correct |
205 ms |
18272 KB |
Output is correct |
15 |
Correct |
139 ms |
18272 KB |
Output is correct |
16 |
Correct |
141 ms |
18272 KB |
Output is correct |
17 |
Correct |
27 ms |
18272 KB |
Output is correct |
18 |
Correct |
23 ms |
18272 KB |
Output is correct |
19 |
Correct |
24 ms |
18272 KB |
Output is correct |
20 |
Correct |
18 ms |
18272 KB |
Output is correct |
21 |
Correct |
21 ms |
18272 KB |
Output is correct |
22 |
Correct |
19 ms |
18272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18272 KB |
Output is correct |
2 |
Correct |
9 ms |
18272 KB |
Output is correct |
3 |
Correct |
9 ms |
18272 KB |
Output is correct |
4 |
Correct |
7 ms |
18272 KB |
Output is correct |
5 |
Correct |
6 ms |
18272 KB |
Output is correct |
6 |
Correct |
8 ms |
18272 KB |
Output is correct |
7 |
Correct |
7 ms |
18272 KB |
Output is correct |
8 |
Correct |
7 ms |
18272 KB |
Output is correct |
9 |
Correct |
7 ms |
18272 KB |
Output is correct |
10 |
Correct |
7 ms |
18272 KB |
Output is correct |
11 |
Correct |
8 ms |
18272 KB |
Output is correct |
12 |
Correct |
11 ms |
18272 KB |
Output is correct |
13 |
Correct |
7 ms |
18272 KB |
Output is correct |
14 |
Correct |
7 ms |
18272 KB |
Output is correct |
15 |
Correct |
9 ms |
18272 KB |
Output is correct |
16 |
Correct |
9 ms |
18272 KB |
Output is correct |
17 |
Correct |
8 ms |
18272 KB |
Output is correct |
18 |
Correct |
7 ms |
18272 KB |
Output is correct |
19 |
Correct |
9 ms |
18272 KB |
Output is correct |
20 |
Correct |
8 ms |
18272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
18272 KB |
Output is correct |
2 |
Correct |
234 ms |
18272 KB |
Output is correct |
3 |
Correct |
218 ms |
18272 KB |
Output is correct |
4 |
Correct |
208 ms |
18272 KB |
Output is correct |
5 |
Correct |
189 ms |
18272 KB |
Output is correct |
6 |
Correct |
247 ms |
21212 KB |
Output is correct |
7 |
Correct |
288 ms |
21212 KB |
Output is correct |
8 |
Correct |
304 ms |
21212 KB |
Output is correct |
9 |
Correct |
278 ms |
21212 KB |
Output is correct |
10 |
Correct |
224 ms |
21212 KB |
Output is correct |
11 |
Correct |
181 ms |
21212 KB |
Output is correct |
12 |
Correct |
245 ms |
21212 KB |
Output is correct |
13 |
Correct |
224 ms |
21212 KB |
Output is correct |
14 |
Correct |
222 ms |
21212 KB |
Output is correct |
15 |
Correct |
168 ms |
21212 KB |
Output is correct |
16 |
Correct |
104 ms |
21212 KB |
Output is correct |
17 |
Correct |
140 ms |
21212 KB |
Output is correct |
18 |
Correct |
191 ms |
21212 KB |
Output is correct |
19 |
Correct |
159 ms |
21212 KB |
Output is correct |
20 |
Correct |
142 ms |
21212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
21212 KB |
Output is correct |
2 |
Correct |
7 ms |
21212 KB |
Output is correct |
3 |
Correct |
7 ms |
21212 KB |
Output is correct |
4 |
Correct |
9 ms |
21212 KB |
Output is correct |
5 |
Correct |
9 ms |
21212 KB |
Output is correct |
6 |
Correct |
7 ms |
21212 KB |
Output is correct |
7 |
Correct |
9 ms |
21212 KB |
Output is correct |
8 |
Correct |
7 ms |
21212 KB |
Output is correct |
9 |
Correct |
10 ms |
21212 KB |
Output is correct |
10 |
Correct |
10 ms |
21212 KB |
Output is correct |
11 |
Correct |
8 ms |
21212 KB |
Output is correct |
12 |
Correct |
7 ms |
21212 KB |
Output is correct |
13 |
Correct |
10 ms |
21212 KB |
Output is correct |
14 |
Correct |
8 ms |
21212 KB |
Output is correct |
15 |
Correct |
8 ms |
21212 KB |
Output is correct |
16 |
Correct |
9 ms |
21212 KB |
Output is correct |
17 |
Correct |
8 ms |
21212 KB |
Output is correct |
18 |
Correct |
6 ms |
21212 KB |
Output is correct |
19 |
Correct |
8 ms |
21212 KB |
Output is correct |
20 |
Correct |
8 ms |
21212 KB |
Output is correct |
21 |
Correct |
7 ms |
21212 KB |
Output is correct |
22 |
Correct |
9 ms |
21212 KB |
Output is correct |
23 |
Correct |
7 ms |
21212 KB |
Output is correct |
24 |
Correct |
8 ms |
21212 KB |
Output is correct |
25 |
Correct |
8 ms |
21212 KB |
Output is correct |
26 |
Correct |
8 ms |
21212 KB |
Output is correct |
27 |
Correct |
8 ms |
21212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
21212 KB |
Output is correct |
2 |
Correct |
269 ms |
21212 KB |
Output is correct |
3 |
Correct |
247 ms |
21212 KB |
Output is correct |
4 |
Correct |
238 ms |
21212 KB |
Output is correct |
5 |
Correct |
230 ms |
21212 KB |
Output is correct |
6 |
Correct |
182 ms |
21212 KB |
Output is correct |
7 |
Correct |
174 ms |
21212 KB |
Output is correct |
8 |
Correct |
192 ms |
21212 KB |
Output is correct |
9 |
Correct |
172 ms |
21212 KB |
Output is correct |
10 |
Correct |
182 ms |
21212 KB |
Output is correct |
11 |
Correct |
193 ms |
21212 KB |
Output is correct |
12 |
Correct |
191 ms |
21212 KB |
Output is correct |
13 |
Correct |
148 ms |
21212 KB |
Output is correct |
14 |
Correct |
139 ms |
21212 KB |
Output is correct |
15 |
Correct |
309 ms |
21212 KB |
Output is correct |
16 |
Correct |
226 ms |
21212 KB |
Output is correct |
17 |
Correct |
214 ms |
21212 KB |
Output is correct |
18 |
Correct |
280 ms |
21456 KB |
Output is correct |
19 |
Correct |
174 ms |
21456 KB |
Output is correct |
20 |
Correct |
240 ms |
22380 KB |
Output is correct |
21 |
Correct |
177 ms |
24012 KB |
Output is correct |
22 |
Correct |
215 ms |
25256 KB |
Output is correct |
23 |
Correct |
163 ms |
26400 KB |
Output is correct |
24 |
Correct |
180 ms |
28920 KB |
Output is correct |
25 |
Correct |
182 ms |
30544 KB |
Output is correct |
26 |
Correct |
272 ms |
31512 KB |
Output is correct |
27 |
Correct |
263 ms |
32844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5220 KB |
Output is correct |
3 |
Correct |
8 ms |
5220 KB |
Output is correct |
4 |
Correct |
7 ms |
5220 KB |
Output is correct |
5 |
Correct |
6 ms |
5296 KB |
Output is correct |
6 |
Correct |
8 ms |
5340 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5220 KB |
Output is correct |
3 |
Correct |
8 ms |
5220 KB |
Output is correct |
4 |
Correct |
7 ms |
5220 KB |
Output is correct |
5 |
Correct |
6 ms |
5296 KB |
Output is correct |
6 |
Correct |
8 ms |
5340 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |