bzoj1123(割点加路径统计)
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; struct my{ int next; int v; }; const int maxn=100000+10; ll low[maxn],dfsn[maxn],size_[maxn],adj[maxn],fa,dfn,root,ans[maxn]; my bian[maxn*10]; bool cut[maxn]; int m,n; void myinsert(int u,int v){ bian[++fa].v=v; bian[fa].next=adj[u]; adj[u]=fa; } void tarjan(ll x){ dfsn[x]=low[x]=++dfn; ll ch=0,sum=0; size_[x]=1; for (int i=adj[x];i;i=bian[i].next){ ll v=bian[i].v; if(!dfsn[v]){ tarjan(v); size_[x]+=size_[v]; low[x]=min(low[x],low[v]); if(dfsn[x]<=low[v]){ ch++; ans[x]+=size_[v]*(n-size_[v]); sum+=size_[v]; if(root!=x||ch>1){ cut[x]=1; } } } else low[x]=min(low[x],dfsn[v]); } if(cut[x]) ans[x]+=(n-sum-1)*(sum+1)+(n-1); else ans[x]=2*(n-1); } int main(){ int u,v; scanf("%d%d",&n,&m); for (int i=1;i<=m;i++){ scanf("%d%d",&u,&v); myinsert(u,v); myinsert(v,u); } for (int i=1;i<=n;i++){ if(!dfsn[i]){ root=i; tarjan(i); } } for (int i=1;i<=n;i++){ printf("%lld\n",ans[i]); } return 0; }