This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F first
#define S second
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>
const int N = 2e5+10;
const int M = 200;
const LL INF = 1e9;
const LL LINF = 1e14;
const LL MOD = 1e9+7;
const double PI = 3.141592653589793;
vector<int>adj[N];
LL sizes[N],depth[N],mx[N],mn[N],n,m,par[N];
bool vis[N];
int dfs1(int i,int pre){
par[i] = pre;
sizes[i] = vis[i] = 1;
for(auto x:adj[i])
if(!vis[x])
depth[x] = depth[i]+1,sizes[i] += dfs1(x,i);
else if(x!=pre){
int cur = i;
while(depth[cur]>=depth[x])mx[cur] = x,mn[cur] = i,cur = par[cur];
}
return sizes[i];
}
LL C(LL n,LL k){
LL ret = 1,d = 1;
for(int i=n;i>=n-k+1;i--)
ret = ret*i,d *= n-i+1;
return ret/d;
}
LL dfs(int i,int pre,int sz){
LL ret = 0;
for(auto x:adj[i])
if(depth[x]==depth[i]+1)
ret += dfs(x,i,sz);
LL cur = sz-1;
for(auto x:adj[i]){
if(depth[x]==depth[i]+1||x==pre){
LL v = 0;
if(x==pre)v = sz-sizes[i];
else v = sizes[x];
ret += (cur-v)*v;
}
else if(depth[x]<depth[i]){
LL len = depth[i]-depth[x]+1;
ret += C(len,3)*4;
LL val = 0;
for(auto x:adj[mx[i]])
if(depth[x]==depth[mx[i]]+1&&mx[x]!=mx[i])
val += sizes[x];
ret += (sz-sizes[mx[i]]+1+val)*(sizes[mx[i]]-sizes[i]-len+1-val)*2;
ret += (sizes[mx[i]]-val-sizes[i]-len+1)*(sizes[i])*2;
}
}
if(mx[i]&&mx[i]!=i){
LL val = 0;
for(auto x:adj[mx[i]])
if(depth[x]==depth[mx[i]]+1&&mx[x]!=mx[i])
val += sizes[x];
LL len = -depth[mx[i]]+depth[i]-1;
ret += len*(sz-sizes[mx[i]]+val)*2;
}
if(mn[i]&&mn[i]!=i){
LL len = depth[mn[i]]-depth[i]-1;
ret += len*(sizes[mn[i]]-1)*2;
}
return ret;
}
bool vis1[N];
bool check(int v1,int v2,int v3,int i){
if(vis1[i])
return 0;
if(i==v3)
return vis1[v2];
vis1[i] = 1;
bool ret = 0;
for(auto x:adj[i])
ret |= check(v1,v2,v3,x);
vis1[i] = 0;
return ret;
}
int main(){
//freopen("out.txt","w",stdout);
//freopen("out.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
depth[0] = -1;
LL ans = 0;
for(int i=1;i<=n;i++)
if(!vis[i]){
int sz = dfs1(i,0);
ans += dfs(i,0,sz);
}
// LL ans1 = 0;
// for(int i=1;i<=n;i++)
// for(int j=1;j<=n;j++)
// for(int k=1;k<=n;k++)
// if(i!=j&&i!=k&&j!=k)
// ans1 += check(i,j,k,i);
printf("%lld\n",ans);
}
Compilation message (stderr)
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:119:13: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
119 | scanf("%d%d",&n,&m);
| ~^ ~~
| | |
| int* long long int*
| %lld
count_triplets.cpp:119:15: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
119 | scanf("%d%d",&n,&m);
| ~^ ~~
| | |
| int* long long int*
| %lld
count_triplets.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
119 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
count_triplets.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
123 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |