BZOJ 1123: [POI2008]BLO

题目大意:

给定一张无向图,求删去一个点以后有多少个有序点对不连通。

题解:
在Tarjan的过程中记录不能到达这个节点以上的子树和,统计答案。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,ti,cnt,last[1000005],dfn[1000005],low[1000005],sz[1000005];
long long ans[1000005];
struct node{
	int to,next;
}e[1000005];
void add(int a,int b){
	e[++cnt].to=b;
	e[cnt].next=last[a];
	last[a]=cnt;
}
void Tarjan(int x,int fa){
	int cnt=0;
	dfn[x]=low[x]=++ti;
	sz[x]=1;
	for (int i=last[x]; i; i=e[i].next){
		int V=e[i].to;
		if (V==fa) continue;
		if (!dfn[V]){
			Tarjan(V,x);
			sz[x]+=sz[V];
			low[x]=min(low[x],low[V]);
			if (low[V]>=dfn[x]){
				ans[x]+=1ll*cnt*sz[V];
				cnt+=sz[V];
			}
		}
		else if (sz[V]) low[x]=min(low[x],dfn[V]);
	}
	ans[x]+=1ll*cnt*(n-cnt-1);
}
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1; i<=m; i++){
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	Tarjan(1,0);
	for (int i=1; i<=n; i++)
		printf("%lld
",(ans[i]+n-1)*2);
	return 0;
}

  

原文地址:https://www.cnblogs.com/silenty/p/8893571.html