# include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, m, used[4][N], root;
int tin[N], fup[N], timer;
int p[N], sz[N], siz[N];
long long ans;
map < pair <int, int>, bool > isb;
vector <int> g[N], gr[N], vr[N];
void dfs(int v, int pr = 0){
used[0][v] = 1;
tin[v] = fup[v] = ++ timer;
for(int to : g[v]){
if(to == pr) continue;
if(used[0][to])
fup[v] = min(fup[v], tin[to]);
else {
dfs(to, v);
fup[v] = min(fup[v], fup[to]);
if(fup[to] > tin[v]){
isb[{v, to}] = 1;
isb[{to, v}] = 1;
}
}
}
}
int get(int v){
return (p[v] == v)?v:p[v] = get(p[v]);
}
void unite(int a, int b){
a = get(a);
b = get(b);
if(a != b){
if(sz[b] > sz[a]) swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
void dfs1(int v){
used[1][v] = 1;
for(int to : g[v]){
if(used[1][to]) continue;
if(!isb[{v, to}])
unite(v, to);
dfs1(to);
}
}
void dfs2(int v){
used[2][v] = 1;
vr[get(v)].push_back(v);
for(int to : g[v]){
if(used[2][to]) continue;
if(get(v) != get(to)){
gr[get(v)].push_back(get(to));
gr[get(to)].push_back(get(v));
}
dfs2(to);
}
}
void dfs3(int v){
used[3][v] = 1;
sort(gr[v].begin(), gr[v].end());
gr[v].resize(unique(gr[v].begin(), gr[v].end()) - gr[v].begin());
siz[v] = sz[v];
for(int to : gr[v]){
if(used[3][to]) continue;
dfs3(to);
siz[v] += siz[to];
}
}
void dfs4(int v, int pr = 0){
ans += sz[v] * 1ll * (sz[v] - 1) * 1ll * (sz[v] - 2);
ans += (siz[root] - siz[v]) * 1ll * (siz[v] - sz[v]) * 1ll * sz[v] * 1ll * 2;
long long cost = (sz[v] - 1) + (sz[v] - 1) * 1ll * (sz[v] - 2), S = 0;
cost *= 2;
ans += (siz[root] - siz[v]) * 1ll * cost;
vector <int> vv;
for(int i : vr[v]){
vector <int> vec;
for(int to : g[i])
if(get(to) != v && get(to) != pr)
vec.push_back(get(to));
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
int sum = 0;
for(int j : vec)
sum += siz[j];
for(int j : vec)
ans += siz[j] * 1ll * (sum - siz[j]);
vv.push_back(sum);
S += sum;
}
for(int i : vv)
ans += i * 1ll * (S - i);
for(int to : gr[v]){
if(to == pr) continue;
ans += siz[to] * 1ll * cost;
dfs4(to, v);
}
}
int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i ++){
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= n; i ++)
p[i] = i, sz[i] = 1;
for(int i = 1; i <= n; i ++){
if(!used[0][i]){
dfs(i);
dfs1(i);
dfs2(i);
root = get(i);
dfs3(get(i));
dfs4(get(i));
}
}
cout << ans << endl;
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:113:12: 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:117:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7384 KB |
Output is correct |
2 |
Correct |
9 ms |
7404 KB |
Output is correct |
3 |
Correct |
10 ms |
7436 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7584 KB |
Output is correct |
6 |
Correct |
9 ms |
7588 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7384 KB |
Output is correct |
2 |
Correct |
9 ms |
7404 KB |
Output is correct |
3 |
Correct |
10 ms |
7436 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7584 KB |
Output is correct |
6 |
Correct |
9 ms |
7588 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
31840 KB |
Output is correct |
2 |
Correct |
268 ms |
31840 KB |
Output is correct |
3 |
Correct |
425 ms |
35344 KB |
Output is correct |
4 |
Correct |
350 ms |
35344 KB |
Output is correct |
5 |
Correct |
395 ms |
35344 KB |
Output is correct |
6 |
Correct |
425 ms |
35344 KB |
Output is correct |
7 |
Correct |
407 ms |
35344 KB |
Output is correct |
8 |
Correct |
364 ms |
35344 KB |
Output is correct |
9 |
Correct |
448 ms |
35344 KB |
Output is correct |
10 |
Correct |
447 ms |
35344 KB |
Output is correct |
11 |
Correct |
282 ms |
35344 KB |
Output is correct |
12 |
Correct |
317 ms |
35344 KB |
Output is correct |
13 |
Correct |
258 ms |
35344 KB |
Output is correct |
14 |
Correct |
227 ms |
35344 KB |
Output is correct |
15 |
Correct |
188 ms |
35344 KB |
Output is correct |
16 |
Correct |
279 ms |
35344 KB |
Output is correct |
17 |
Correct |
27 ms |
35344 KB |
Output is correct |
18 |
Correct |
29 ms |
35344 KB |
Output is correct |
19 |
Correct |
34 ms |
35344 KB |
Output is correct |
20 |
Correct |
31 ms |
35344 KB |
Output is correct |
21 |
Correct |
31 ms |
35344 KB |
Output is correct |
22 |
Correct |
31 ms |
35344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
35344 KB |
Output is correct |
2 |
Correct |
10 ms |
35344 KB |
Output is correct |
3 |
Correct |
10 ms |
35344 KB |
Output is correct |
4 |
Correct |
10 ms |
35344 KB |
Output is correct |
5 |
Correct |
9 ms |
35344 KB |
Output is correct |
6 |
Correct |
9 ms |
35344 KB |
Output is correct |
7 |
Correct |
11 ms |
35344 KB |
Output is correct |
8 |
Correct |
11 ms |
35344 KB |
Output is correct |
9 |
Correct |
12 ms |
35344 KB |
Output is correct |
10 |
Correct |
11 ms |
35344 KB |
Output is correct |
11 |
Correct |
12 ms |
35344 KB |
Output is correct |
12 |
Correct |
10 ms |
35344 KB |
Output is correct |
13 |
Correct |
10 ms |
35344 KB |
Output is correct |
14 |
Correct |
10 ms |
35344 KB |
Output is correct |
15 |
Correct |
9 ms |
35344 KB |
Output is correct |
16 |
Correct |
10 ms |
35344 KB |
Output is correct |
17 |
Correct |
10 ms |
35344 KB |
Output is correct |
18 |
Correct |
10 ms |
35344 KB |
Output is correct |
19 |
Correct |
12 ms |
35344 KB |
Output is correct |
20 |
Correct |
13 ms |
35344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
537 ms |
35344 KB |
Output is correct |
2 |
Correct |
546 ms |
35344 KB |
Output is correct |
3 |
Correct |
476 ms |
35344 KB |
Output is correct |
4 |
Correct |
457 ms |
35344 KB |
Output is correct |
5 |
Correct |
530 ms |
35344 KB |
Output is correct |
6 |
Correct |
583 ms |
44132 KB |
Output is correct |
7 |
Correct |
609 ms |
44132 KB |
Output is correct |
8 |
Correct |
543 ms |
44132 KB |
Output is correct |
9 |
Correct |
470 ms |
44132 KB |
Output is correct |
10 |
Correct |
413 ms |
44132 KB |
Output is correct |
11 |
Correct |
440 ms |
44132 KB |
Output is correct |
12 |
Correct |
472 ms |
44132 KB |
Output is correct |
13 |
Correct |
405 ms |
44132 KB |
Output is correct |
14 |
Correct |
365 ms |
44132 KB |
Output is correct |
15 |
Correct |
341 ms |
44132 KB |
Output is correct |
16 |
Correct |
251 ms |
44132 KB |
Output is correct |
17 |
Correct |
402 ms |
44132 KB |
Output is correct |
18 |
Correct |
422 ms |
44132 KB |
Output is correct |
19 |
Correct |
485 ms |
44132 KB |
Output is correct |
20 |
Correct |
535 ms |
44132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
44132 KB |
Output is correct |
2 |
Correct |
10 ms |
44132 KB |
Output is correct |
3 |
Incorrect |
13 ms |
44132 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
445 ms |
44132 KB |
Output is correct |
2 |
Correct |
503 ms |
44132 KB |
Output is correct |
3 |
Incorrect |
492 ms |
44132 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7384 KB |
Output is correct |
2 |
Correct |
9 ms |
7404 KB |
Output is correct |
3 |
Correct |
10 ms |
7436 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7584 KB |
Output is correct |
6 |
Correct |
9 ms |
7588 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7384 KB |
Output is correct |
2 |
Correct |
9 ms |
7404 KB |
Output is correct |
3 |
Correct |
10 ms |
7436 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7584 KB |
Output is correct |
6 |
Correct |
9 ms |
7588 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |