【NOIP2014模拟10.26】战争游戏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Larry1118/article/details/86651812

这题就是求割点并计算答案。
上标:

#include<cstdio>
#include<algorithm>
#define N 50010
using namespace std;
struct node{int v,fr;}e[N<<2];
int n,m,tail[N],cnt=0,tot=0;
int dfn[N],low[N],ans[N],siz[N];

inline int read()
{
	int x=0; char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}

void add(int u,int v) {e[++cnt]=(node){v,tail[u]}; tail[u]=cnt;}

void dfs(int x)
{
	siz[x]=1,dfn[x]=low[x]=++tot;
	int size=0;
	for (int p=tail[x],v;p;p=e[p].fr)
		if (!dfn[v=e[p].v])
		{
			dfs(v),low[x]=min(low[x],low[v]);
			if (low[v]>=dfn[x])
				ans[x]+=(n-siz[v]-1)*siz[v],size+=siz[v];
			siz[x]+=siz[v];	
		}
		else low[x]=min(low[x],dfn[v]);
	ans[x]+=(n-size-1)*size;
}

int main()
{
	n=read(),m=read();
	for (int i=1,u,v;i<=m;i++)
		u=read(),v=read(),add(u,v),add(v,u);
	dfs(1);
	for (int i=1;i<=n;i++) printf("%d\n",ans[i]/2+(n-1));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Larry1118/article/details/86651812