CF219D Choosing Capital for Treeland

CF219D Choosing Capital for Treeland

洛谷传送门

题目描述

The country Treeland consists of nn cities, some pairs of them are connected with unidirectional roads. Overall there are n-1n−1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.

The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city aa is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city aa to any other city. For that some roads may have to be inversed.

Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.

输入格式

The first input line contains integer nn ( 2<=n<=2·10^{5}2<=n<=2⋅105 ) — the number of cities in Treeland. Next n-1n−1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers s_{i},t_{i}s**i,t**i ( 1<=s_{i},t_{i}<=n; s_{i}≠t_{i}1<=s**i,t**i<=n; s**i�=t**i ) — the numbers of cities, connected by that road. The ii -th road is oriented from city s_{i}s**i to city t_{i}t**i . You can consider cities in Treeland indexed from 1 to nn .

输出格式

In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.

题意翻译

题目描述

Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。

输入输出格式

输入格式

输入包含多个测试点。对于每个测试点,每个测试点的第一行为一个正整数n(2<=n<=2e5)。接下来n-1行,每行两个正整数ai,bi,表示城市a到城市b有一条单向通行的道路。输入以空格分隔,以EOF结尾。

输出格式

对于每个测试点,第一行输出k,第二行升序输出所有满足条件的首都的编号。


题解:

思路:

树上最优化:考虑树形DP。需要枚举所有根求树上最优化:考虑换根DP。

所以正解:树形换根DP。

换根DP也叫二次扫描DP,如有不懂的小伙伴请走这边:

浅谈换根DP

所谓二次扫描,当然就是要扫描两次。第一次扫描是从下往上回溯信息,也就是”选择任意节点为根的树形DP预处理“。第二次扫描是从上往下更新信息,也就是”改变树的形态,祖先变儿子的换根DP“。

关键在于找出两次扫描的转移方程。

第一次扫描的转移方程是比较好想的,只需要判断当前枚举到的这条边需不需要反向即可。需要反向就加一,不需要反向就不动。

第二次扫描呢?

对于当前节点y来讲,它目前维护的以1为初根的dp[y]是正确的,也就是我们只需要统计它原来的祖先会对它造成什么影响。容易根据上一次转移发现的性质是:它父亲的答案是包含它的,也就是,对于一对父子(x,y)来讲,只需要考虑他们中间的边在换根后需不需要转向。

原来不需要,现在就需要,原来需要,现在就不需要。

于是我们也构思出了另外一个转移方程。

剩下的细节可以详见代码。再需要墨迹的一个小技巧就是对于这种有向图判断反转边的问题,可以考虑把双向边都存进去,也就是存两条边((u,v,0),(v,u,1))表示这条边需不需要反转。

那么代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=2*1e5+10;
const int INF=1e9;
int n,cnt,ans;
int tot,to[maxn<<1],nxt[maxn<<1],val[maxn<<1],head[maxn];
int pos[maxn],dp[maxn];
//dp[i]表示以i为根子树需要转多少边才能转回去
void add(int x,int y,int z)
{
	to[++tot]=y;
	nxt[tot]=head[x];
	val[tot]=z;
	head[x]=tot;
}
void dfs1(int x,int f)
{
	for(int i=head[x];i;i=nxt[i])
	{
		int y=to[i];
		if(y==f)
			continue;
		dfs1(y,x);
		dp[x]+=(dp[y]+val[i]);
	}
}
void dfs2(int x,int f)
{
	for(int i=head[x];i;i=nxt[i])
	{
		int y=to[i];
		if(y==f)
			continue;
		dp[y]=dp[x]+pow(-1,val[i]);
		dfs2(y,x);
	}
	if(dp[x]<ans)
		ans=dp[x],cnt=0;
	if(dp[x]==ans)
		pos[++cnt]=x;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		cnt=0;tot=0;ans=INF;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<n;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y,0);
			add(y,x,1);
		}
		dfs1(1,0);
		dfs2(1,0);
		printf("%d
",ans);
		sort(pos+1,pos+cnt+1);
		for(int i=1;i<=cnt;i++)
			printf("%d ",pos[i]);
		puts("");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/fusiwei/p/13856522.html