Description
Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通。
Input
输入n<=100000 m<=500000及m条边
Output
输出n个数,代表如果把第i个点去掉,将有多少对点不能互通。
Sample Input
5 5
1 2
2 3
1 3
3 4
4 5
1 2
2 3
1 3
3 4
4 5
Sample Output
8
8
16
14
8
8
16
14
8
Solotion
Tarjan求割点,再记一下这个割点以下的点的个数。
|
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 |
#include<iostream> #include<cstdio> using namespace std; const int N=100100,M=500500; int n,m,tot,cnt,Next[M*2],head[N],tree[M*2],dfn[N],low[N]; long long size[N],ans[N]; void add(int x,int y) { tot++; Next[tot]=head[x]; head[x]=tot; tree[tot]=y; } void Tarjan(int u) { dfn[u]=low[u]=++cnt; size[u]=1; long long t=0; for (int i=head[u];i;i=Next[i]) { int v=tree[i]; if (dfn[v]) low[u]=min(low[u],dfn[v]); else { Tarjan(v); low[u]=min(low[u],low[v]); size[u]+=size[v]; if (low[v]>=dfn[u]) { ans[u]+=t*size[v]; t+=size[v]; } } } ans[u]+=t*(n-t-1)+(n-1); } int main() { scanf("%d%d",&n,&m); tot=cnt=0; for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y);add(y,x); } Tarjan(1); for (int i=1;i<=n;i++) printf("%lld\n",ans[i]*2); return 0; } |