#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
long long n,m,a[MAXN],b[MAXN];
long long sz[MAXN],szcomp[MAXN],total[MAXN],rem[MAXN];
int comp[MAXN],k;
vector< pair<int,int> > v[MAXN];
vector<int> tree[MAXN];
long long ans,sum[MAXN];
int dep[MAXN],low[MAXN];
bool li[MAXN],bridge[MAXN];
void bcc(int x,int p,int d,int e){
li[x]=true; dep[x]=d; low[x]=dep[x];
for(int i=0;i<v[x].size();i++){
if(!li[v[x][i].first]){
bcc(v[x][i].first,x,d+1,v[x][i].second);
low[x]=min(low[x],low[v[x][i].first]);
}else if(v[x][i].first!=p){
low[x]=min(low[x],dep[v[x][i].first]);
}
}
if(p!=0 and low[x]==dep[x]){
bridge[e]=true;
}
}
void dfs(int x,int c){
li[x]=true; comp[x]=c;
szcomp[c]++;
for(int i=0;i<v[x].size();i++){
if(li[v[x][i].first] or bridge[v[x][i].second])continue;
dfs(v[x][i].first,c);
}
}
int num[MAXN],w,last[MAXN];
void dfs2(int x,int p){
sz[x]=szcomp[x];
w++; num[x]=w;
sum[x]=szcomp[x]*(szcomp[x]-1);
for(int i=0;i<tree[x].size();i++){
if(tree[x][i]==p)continue;
dfs2(tree[x][i],x);
sz[x]+=sz[tree[x][i]];
sum[x]+=sum[tree[x][i]];
}
last[x]=w;
}
int root[MAXN];
void dfs3(int x,int p,int t,int r){
total[x]=t; root[x]=r;
for(int i=0;i<tree[x].size();i++){
if(tree[x][i]==p)continue;
dfs3(tree[x][i],x,t,r);
}
}
bool check(int x,int y){
return num[y]>=num[x] and num[y]<=last[x];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i];
v[a[i]].push_back({b[i],i});
v[b[i]].push_back({a[i],i});
}
for(int i=1;i<=n;i++){
if(!li[i])bcc(i,0,0,0);
}
for(int i=1;i<=n;i++)li[i]=false;
for(int i=1;i<=n;i++){
if(!li[i]){
k++; dfs(i,k);
}
}
for(int i=1;i<=m;i++){
if(comp[a[i]]==comp[b[i]])continue;
tree[comp[a[i]]].push_back(comp[b[i]]);
tree[comp[b[i]]].push_back(comp[a[i]]);
}
for(int i=1;i<=k;i++){
if(sz[i]==0){
dfs2(i,0);
dfs3(i,0,sz[i],i);
}
}
for(int i=1;i<=m;i++){
if(comp[a[i]]==comp[b[i]])continue;
if(sz[comp[a[i]]]<sz[comp[b[i]]])swap(a[i],b[i]);
rem[a[i]]+=sz[comp[b[i]]];
rem[b[i]]+=total[comp[b[i]]]-sz[comp[b[i]]];
}
/*
for(int i=1;i<=k;i++){
ans+=( (total[i])*(total[i]-1) - sum[root[i]] - szcomp[i]*(total[i]-szcomp[i])*2 ) * szcomp[i];
for(int f=0;f<tree[i].size();f++){
if(sz[tree[i][f]]<sz[i]){
ans-=(sz[tree[i][f]]*(sz[tree[i][f]]-1) - sum[tree[i][f]] )*szcomp[i];
}else{
ans-=( ( (total[i]-sz[i])*(total[i]-sz[i]-1) - (sum[root[i]] - sum[i]) ) )*szcomp[i];
}
}
}
*/
for(int i=1;i<=n;i++){
for(int f=1;f<=n;f++){
for(int t=1;t<=n;t++){
if(comp[i]==comp[f] or comp[f]==comp[t] or comp[i]==comp[t])continue;
if(root[comp[i]]!=root[comp[f]] or root[comp[i]]!=root[comp[t]] or root[comp[f]]!=root[comp[t]])continue;
ans++;
for(int z:tree[comp[f]]){
if(sz[z]<sz[comp[f]] and check(comp[z],comp[i]) and check(comp[z],comp[t])){ans--; break;}
if(sz[z]>sz[comp[f]] and !check(comp[f],comp[i]) and !check(comp[f],comp[t])){ans--; break;}
}
}
}
}
for(int i=1;i<=n;i++){
ans+=(szcomp[comp[i]]-1)*(total[comp[i]]-szcomp[comp[i]]-rem[i])*2;
}
for(int i=1;i<=k;i++){
ans+=szcomp[i]*(szcomp[i]-1)*(szcomp[i]-2);
}
cout<<ans<<"\n";
return 0;
}
Compilation message
count_triplets.cpp: In function 'void bcc(int, int, int, int)':
count_triplets.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs(int, int)':
count_triplets.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:53:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<tree[x].size();i++){
| ~^~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs3(int, int, int, int)':
count_triplets.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0;i<tree[x].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1039 ms |
32596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1028 ms |
21084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
30036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
21080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1070 ms |
30036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Incorrect |
3 ms |
20828 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |