[POI2008]BLO-Blockade

题意翻译

在Byteotia有n个城镇。 一些城镇之间由无向边连接。 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些)。每两个城镇之间至多只有一条直接连接的道路。人们可以从任意一个城镇直接或间接到达另一个城镇。 每个城镇都有一个公民,他们被孤独所困扰。事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇)。所以,一共会有n*(n-1)次拜访。

不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件。

作为抗议行动,程序员们计划封锁一些城镇,阻止人们进入,离开或者路过那里。

正如我们所说,他们正在讨论选择哪些城镇会导致最严重的后果。

编写一个程序:

读入Byteotia的道路系统,对于每个被决定的城镇,如果它被封锁,有多少访问不会发生,输出结果。

输入输出格式

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

城镇编号1~n

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

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

题目描述

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.

 

输出格式:

 

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

 

输入输出样例

输入样例#1: 

5 5
1 2
2 3
1 3
3 4
4 5

输出样例#1: 

8
8
16
14
8

 

对于每一个点,可以分为两种情况

1.

这个点不是是割点(去掉这个点以后原图不联通),那么结果就为 (n-1) * 2 , 就是这个点的居民不能访问别的人,别的人也不能访问这个人

2.

如果这是割点,那么去掉以后就会支离破碎

先定义一个size[u]表示以u为根的子树中有多少个节点

那么在u为割点的情况下,对于每一个子树,我们都

ans[i] = ans[i] + size[v] * ( n - size[v] ) ;

说明这一个子树中的居民(不包括u)都不能和非子树(这里包括u,因为u被关在里面了嘛)的居民不能来往

并且顺便判断割点

如果这样做,那是肯定会错的

 

因为我们只考虑到了非割点的情况

以u为根的子树对外界的来往

而没有考虑到外界对这可树的来往

于是我们定义一个ss数组,表示以i为根的子树的点的数量-1(不包括u,并且当且仅当u对于这棵子树来说是割点的情况)

这里要注意一点,ss != size[u] ,想想这是为什么

所以外界对这可树的来往就要

ans[u] =  ans[u] + ( n - ss - 1 ) * ( ss + 1 ) + ( n - 1 )

// ( n - ss - 1 ) * ( ss + 1 ) 表示树之外的点对子树的来往

// ( n - 1 ) 表示u对其他点的来往

因为全部点都是联通的,所以我们只用Tarjan(1)就ok了

代码 ( 希望能够帮到您)

#include <iostream>
#include <cstring>

using namespace std ;

typedef long long LL ;

const int N = 1e5 + 10 ;
const int M = 5e5 + 10 ;

struct edge { 
	int v , nxt ;
}e[M<<1] ; int tot , last[N] ;
inline void add ( int u , int v ) {
	e[++tot] = (edge){ v , last[u] } ;
	last[u] = tot ;
	e[++tot] = (edge){ u , last[v] } ;
	last[v] = tot ;
}
	
int n , m ;

int id , dfn[N] , low[N] ;
int size[N] ; LL ans[N] ;
bool gedian[N] ; 

void Tarjan ( int u ) {
	dfn[u] = low[u] = ++id ;
	size[u] = 1 ; int cnt = 0 , ss = 0 ;
	for ( int i = last[u] ; i != -1 ; i = e[i].nxt ) {
		int v = e[i].v ;
		if ( dfn[v] == 0 ) {
			Tarjan( v ) ; size[u] += size[v] ;
			low[u] = min ( low[u] , low[v] ) ;
			if ( dfn[u] <= low[v] ) {
				cnt ++ ; ss += size[v] ;
				ans[u] += (LL)size[v] * ( n - size[v] ) ;
				if ( u != 1 || cnt > 1 ) gedian[u] = 1 ;
			}		
		}
		else low[u] = min ( low[u] , dfn[v] ) ;
	}
	if ( gedian[u] == 0 ) ans[u] = 2 * ( n-1 ) ;
	else ans[u] += (LL)( n - ss - 1 ) * ( ss + 1 ) + ( n - 1 ) ;
}
int main() {
	cin >> n >> m ; int u , v ;
	tot = -1 , memset ( last , -1 , sizeof( last ) ) ;
	for ( int i = 1 ; i <= m ; i ++ )
		cin >> u >> v , add( u , v ) ;
	memset ( gedian , 0 , sizeof( gedian ) ) ;
	Tarjan( 1 ) ;
	for ( int i = 1 ; i <= n ; i ++ ) 
		cout << ans[i] << endl ;
	return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值