#include <bits/stdc++.h>
using namespace std;
typedef pair<long long,long long> PLL;
typedef long long LL;
#define int long long
#define ln '\n'
#define faster {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define all(v) v.begin(),v.end()
int mod = 998244353;
const int N = 4e5 + 10;
const int inf = 1e9;
vector<PLL> g[N];
vector<int> comp[N];
int disc[N], low[N], mark[N], vis[N], timer = 1, cur = 1;
vector<int> currcomp;
void dfs(int u, int p){
disc[u] = low[u] = timer++; currcomp.push_back(u);
bool fl = 1;
for(auto [v, id]: g[u]){
if(v == p && fl) {fl = 0; continue;}
if(disc[v]){
low[u] = min(low[u], disc[v]);
}else{
dfs(v, u);
low[u] = min(low[u], low[v]);
if(disc[u] < low[v]){
mark[id] = 1;
}
}
}
}
void colorComponents(int u, int color){
if(vis[u]) return;
comp[color].push_back(u);
vis[u] = color;
for(auto [v, id]: g[u]){
if(mark[id]) continue;
colorComponents(v, color);
}
}
void solve(int test){
int n, m, q; cin>>n>>m;
for(int i = 0, u, v; i < m; i++){
cin>>u>>v;
g[u].push_back({v, i}); g[v].push_back({u, i});
}
auto nC2 = [&](LL n){
return n * (n - 1) / 2;
};
auto nC3 = [&](LL n){
return n * (n - 1) * (n - 2) / 6;
};
LL ans = 0;
for(int i = 1; i <= n; i++){
if(!disc[i]){
dfs(i, -1);
int SZ = currcomp.size(), last = cur;
ans += nC3(SZ);
for(auto u: currcomp){
if(!vis[u]){
colorComponents(u, cur++);
}
}
for(int j = last; j < cur; j++){
int sz = comp[j].size(); //cout<<sz<<'\n';
LL tmp = nC3(sz) * 2 + nC2(sz - 1) * (SZ - sz);
ans += tmp;
}
}
}
cout<<ans * 2<<'\n';
}
signed main(){
faster
int t = 1;
// cin>>t;
for(int i = 1; i <= t; i++){
solve(i);
}
return 0;
}
Compilation message
count_triplets.cpp: In function 'void solve(long long int)':
count_triplets.cpp:52:15: warning: unused variable 'q' [-Wunused-variable]
52 | int n, m, q; cin>>n>>m;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
21336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
21336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
36808 KB |
Output is correct |
2 |
Correct |
47 ms |
37068 KB |
Output is correct |
3 |
Incorrect |
53 ms |
33864 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
19292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
31356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
21336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
33216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
21336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
21336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |