#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) (ll)x.size()
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
using namespace std;
const int N=1e5+5;
vi g[N];stack<int>st;
vvi cmp,bst;
ll d[N]{0},lo[N]{0},t=0,ap[N]{0},id[N]{0},cur=0,isap[N]{0},sz[N],dp[N],vis[N]{0};
void dfs(int u,int p){
lo[u]=d[u]=++t;st.push(u);
for(auto v:g[u]){
if(!d[v]){
dfs(v,u);
lo[u]=min(lo[u],lo[v]);
if(d[u]<=lo[v]){
ap[u] = (d[u]>1||d[v]>2);cmp.pb({u});
while(cmp.back().back()!=v){
cmp.back().pb(st.top());st.pop();
}
}
}
else if(v!=p)lo[u]=min(lo[u],d[v]);
}
}
void build(int n){
for(int i=1;i<=n;i++)if(ap[i])id[i]=cur++,isap[id[i]]=1,bst.pb({}),sz[id[i]]=1;
for(auto it : cmp){
bst.pb({});sz[cur]=it.size();
for(auto ij : it){
if(!ap[ij])id[ij]=cur;
else {
bst[cur].pb(id[ij]);
bst[id[ij]].pb(cur);
}
}cur++;
}
}ll ans=0,n,sss=0;
void dd(int u,int p){
sss+=sz[u];
sss-=(isap[u]?0:sz(bst[u]));
for(auto v:bst[u])if(v!=p)dd(v,u);
}
void solve(int u,int p){vis[u]=1;
dp[u]=sz[u];if(!isap[u])ans+=(sz[u]-sz(bst[u]))*(sz[u]-sz(bst[u])-1)*(sz[u]-sz(bst[u])-2);
ll tt=0,cc=0;
for(auto v:bst[u]){
if(v==p)continue;
solve(v,u);
if(isap[u])ans+=2*(dp[v]-1)*(tt)+(sz[v]-1)*(sz[v]-2);
else ans+=2*(dp[v])*(tt+cc)*(sz[u]-sz(bst[u]));
tt+=dp[v]-1;cc++;
}dp[u]+=tt;
if(!isap[u]&&isap[p])ans+=2*(sss-dp[u]+1)*(sz[u]-sz(bst[u]))*(dp[u]-sz[u]+sz(bst[u])-1)+2*(sz[u]-sz(bst[u]))*(sz[u]-sz(bst[u])-1)*(sss-dp[u]+1)+2*(sz[u]-sz(bst[u]))*(sz[u]-sz(bst[u])-1)*(dp[u]-sz[u]+sz(bst[u])-1);
else if(!isap[u]&&!isap[p])ans+=+2*(sz[u]-sz(bst[u]))*(sz[u]-sz(bst[u])-1)*(dp[u]-sz[u]+sz(bst[u]));
else if(isap[u])ans+=2*(sss-dp[u])*(dp[u]-1);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int m;cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;cin>>u>>v;
g[u].pb(v);g[v].pb(u);
}for(int i=1;i<=n;i++)if(!d[i])dfs(i,i),t=0;
build(n);for(int i=0;i<cur;i++){
if(!vis[i]){
sss=0;dd(i,i);solve(i,i);
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8896 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8896 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
25824 KB |
Output is correct |
2 |
Correct |
53 ms |
25984 KB |
Output is correct |
3 |
Incorrect |
63 ms |
28940 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
8880 KB |
Output is correct |
3 |
Correct |
3 ms |
9048 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
3 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9052 KB |
Output is correct |
14 |
Correct |
3 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
8916 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
2 ms |
9052 KB |
Output is correct |
19 |
Correct |
2 ms |
9048 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
27996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
29184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8896 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8896 KB |
Output is correct |
7 |
Incorrect |
2 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |