仔细观察样例会发现其实(x,y)和(y,x)是不同的…
求割点…然后直接根据乘法原理计算答案就好了…
代码如下:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
const int maxn=100000+5,maxm=500000+5;
int n,m,hd[maxn],to[maxm*2],nxt[maxm*2],cnt,dfn[maxn],low[maxn*2],tim;
long long ans[maxn];
inline void add(int x,int y){
to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
}
inline long long tarjan(int root,int f){
dfn[root]=low[root]=++tim;long long tmp=0,sum=0;ans[root]=(n-1)<<1;int cnt=0,siz;
for(int i=hd[root];i!=-1;i=nxt[i])
if(to[i]!=f){
if(!dfn[to[i]]){
siz=tarjan(to[i],root);low[root]=min(low[root],low[to[i]]);
if(low[to[i]]>=dfn[root])
ans[root]+=2LL*siz*sum,sum+=siz;
else
tmp+=siz;
}
else
low[root]=min(low[root],dfn[to[i]]);
}
ans[root]+=2LL*(n-(sum+1))*sum;
return sum+tmp+1;
}
signed main(void){
tim=cnt=0;
scanf("%d%d",&n,&m);
memset(hd,-1,sizeof(hd));
memset(dfn,0,sizeof(dfn));
for(int i=1,x,y;i<=m;i++)
scanf("%d%d",&x,&y),add(x,y),add(y,x);
tarjan(1,-1);
for(int i=1;i<=n;i++)
printf("%lld\n",ans[i]);
return 0;
}
by >_< NeighThorn