# 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];
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)
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 += sz[j];
for(int j : vec)
ans += sz[j] * 1ll * (sum - sz[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 |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7532 KB |
Output is correct |
3 |
Correct |
9 ms |
7532 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7532 KB |
Output is correct |
6 |
Correct |
9 ms |
7668 KB |
Output is correct |
7 |
Incorrect |
7 ms |
7668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7532 KB |
Output is correct |
3 |
Correct |
9 ms |
7532 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7532 KB |
Output is correct |
6 |
Correct |
9 ms |
7668 KB |
Output is correct |
7 |
Incorrect |
7 ms |
7668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
31828 KB |
Output is correct |
2 |
Correct |
236 ms |
31908 KB |
Output is correct |
3 |
Incorrect |
399 ms |
35388 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
35388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
596 ms |
35388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
35388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
504 ms |
35388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7532 KB |
Output is correct |
3 |
Correct |
9 ms |
7532 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7532 KB |
Output is correct |
6 |
Correct |
9 ms |
7668 KB |
Output is correct |
7 |
Incorrect |
7 ms |
7668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7532 KB |
Output is correct |
3 |
Correct |
9 ms |
7532 KB |
Output is correct |
4 |
Correct |
7 ms |
7532 KB |
Output is correct |
5 |
Correct |
8 ms |
7532 KB |
Output is correct |
6 |
Correct |
9 ms |
7668 KB |
Output is correct |
7 |
Incorrect |
7 ms |
7668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |