1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <bits/stdc++.h> #define LL long long using namespace std;
const int MAXN=5e5+2;
int n,m,dfn[MAXN],low[MAXN],cnt,idx,w[MAXN],sz[MAXN],sz_cur;
int st[MAXN],top;
LL ans;
struct G{ vector<int> p[MAXN]; void add(int u,int v){ p[u].push_back(v); p[v].push_back(u); } vector<int>& operator[](int i){ return p[i]; } }graph,crt;
void tarjan(int x,int fno){ w[x]=-1;++sz_cur; dfn[x]=low[x]=++cnt; st[++top]=x; for(auto v:graph[x]){ if(v==fno)continue; if(!dfn[v]){ tarjan(v,x); low[x]=min(low[x],low[v]); if(low[v]>=dfn[x]){ int vv;++idx; do{ vv=st[top--]; crt.add(idx,vv); ++w[idx]; }while(vv!=v); crt.add(idx,x); ++w[idx]; } }else{ low[x]=min(low[x],dfn[v]); } } }
void dfs(int x,int fno){ sz[x]=x<=n; LL cnt=0; for(auto v:crt[x]){ if(v==fno)continue; dfs(v,x); cnt+=1ll*sz[x]*sz[v]; sz[x]+=sz[v]; } cnt+=1ll*sz[x]*(sz_cur-sz[x]); cnt<<=1; ans+=1ll*cnt*w[x]; }
int main(){ cin>>n>>m; for(int i=1,u,v;i<=m;++i){ cin>>u>>v; graph.add(u,v); } idx=n; for(int i=1;i<=n;++i){ if(!dfn[i]){ tarjan(i,0); dfs(i,0); top=sz_cur=0; } } cout<<ans<<endl; return 0; }
|