Tarjan求割点+数学 [POI2008]BLO-Blockade(洛谷 P3469)

[POI2008]BLO-Blockade

题目描述

There are exactly nn towns in Byteotia.

Some towns are connected by bidirectional roads.

There are no crossroads outside towns, though there may be bridges, tunnels and flyovers. Each pair of towns may be connected by at most one direct road. One can get from any town to any other-directly or indirectly.

Each town has exactly one citizen.

For that reason the citizens suffer from loneliness.

It turns out that each citizen would like to pay a visit to every other citizen (in his host’s hometown), and do it exactly once. So exactly n\cdot (n-1)n⋅(n−1) visits should take place.

That’s right, should.

Unfortunately, a general strike of programmers, who demand an emergency purchase of software, is under way.

As an act of protest, the programmers plan to block one town of Byteotia, preventing entering it, leaving it, and even passing through.

As we speak, they are debating which town to choose so that the consequences are most severe.

Task Write a programme that:

reads the Byteotian road system’s description from the standard input, for each town determines, how many visits could take place if this town were not blocked by programmers, writes out the outcome to the standard output.

给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y

输入格式

In the first line of the standard input there are two positive integers: nn and mm (1\le n\le 100\ 0001≤n≤100 000, 1\le m\le 500\ 0001≤m≤500 000) denoting the number of towns and roads, respectively.

The towns are numbered from 1 to nn.

The following mm lines contain descriptions of the roads.

Each line contains two integers aa and bb (1\le a<b\le n1≤a<b≤n) and denotes a direct road between towns numbered aa and bb.

第一行读入n,m,分别是城镇数目和道路数目

输出格式

Your programme should write out exactly nn integers to the standard output, one number per line. The i^{th}i
th
line should contain the number of visits that could not take place if the programmers blocked the town no. ii.
城镇编号1~n

接下来m行每行两个数字a,b,表示a和b之间有有一条无向边

输出n行,每行一个数字,为第i个城镇被锁时不能发生的访问的数量。


Tarjan求得割点的同时,相当于求出了一条割边,这条边的子树数量要记录一下,后面要计算到;

当求得每个点是否割点后,也就求得了每个割点所连接的割边有几条,每条的子树大小是多少;

然后运用一点数学计算大小即可;

难点:比较难想到是统计每个割点所连接割边的子树大小,数学公式比较好推;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100010;
const int M=1000100;
const LL mod=100000000;
int dfn[N],low[N],tot,head[N],cnt,n,m,vis[N];
vector<LL>ve[N];
struct Node{
	int to,nex;
}edge[M];
void add(int p,int q){
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
int Tarjan(int p,int fa){
	dfn[p]=low[p]=++tot;
	int ch=0;//儿子数量 
	int sum1=1,sum2=1;
	for(int i=head[p];~i;i=edge[i].nex){
		int q=edge[i].to;
		if(!dfn[q]){
			ch++;
			int s=Tarjan(q,p);
			sum1+=s;
			low[p]=min(low[p],low[q]);
			if(p!=fa&&low[q]>=dfn[p]){
				sum2+=s;
				ve[p].push_back(1ll*s);
				vis[p]=1;
			}
			if(p==fa&&ch>=2){
				sum2+=s;
				ve[p].push_back(1ll*s);
				vis[p]=1;
			}
		}
		else if(q!=fa) low[p]=min(low[p],dfn[q]);
	}
	ve[p].push_back(1ll*(n-sum2));
	return sum1;
}
int main(){
	memset(head,-1,sizeof(head));
	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,1);
	for(int i=1;i<=n;i++){
		if(vis[i]){
			LL ans=0;
			for(int j=0;j<ve[i].size();j++){
				ans+=(1ll*n-ve[i][j])*ve[i][j];
			}
			ans+=1ll*(n-1);
			printf("%lld\n",ans);
		}
		else printf("%lld\n",2ll*(n-1));
	}
    return 0;
}

发布了264 篇原创文章 · 获赞 46 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44291254/article/details/104999950