CF-219D Choosing Capital for Treeland(树形dp)

CF-219D

题目描述

The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 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 a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a 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.

Input

The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ n; si ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.

Output

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.

Examples

Input

3
2 1
2 3

Output

0
2 

Input

4
1 4
2 4
3 4

Output

2
1 2 3 

题意

给你一个树,树上的路径是有向路,选一个点作为首都,这个点要满足能到达其他所有的点。因为是有向图,所以选好一个点以后有的路会需要改变方向,问哪一个/些点选做首都之后需要改变方向的路的个数最少。

个人理解

首先,很容易做出来以一个点为根的子树的需要改变方向的路的个数,用一遍dfs遍历,这一次用自底向上的遍历。
然后,第二层更新出来每个点作为首都是需要改变方向的路的个数,这一次是自顶向下的遍历,递推关系如下:
d p [ v ] = d p [ v ] + ( d p [ u ] − d p [ v ] ) + ( d i r [ u ] [ v ] ? 1 : − 1 ) ( v 是 u 的 子 节 点 ) dp[v] =dp[v]+(dp[u]-dp[v])+(dir[u][v]?1:-1)(v是u的子节点) dp[v]=dp[v]+(dp[u]dp[v])(dir[u][v]?1:1)(vu)
这地方简单描述一下:因为我们第二次是自顶向下遍历,所以在更新一个子节点的时候,当前的父节点已经更新完。这个时候以子节点为根的子树的更改个数在第一遍dfs已经得到,是dp[v],而除去这个子树更改个数的父节点的更改个数是dp[u]-dp[v],所以我们只需要看一下父节点和子节点相连的这个边是否需要改变方向就可以了。

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;

struct node{
	int to;
	int dir;
	int next;
}edge[maxn<<1];
int cnt,head[maxn];
int n;
int dp[maxn];

void init(){
	cnt = 0;
	memset(head,-1,sizeof(head));
}

void add(int u,int v,int dir){
	edge[cnt].to = v;
	edge[cnt].dir = dir;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}

void dfs(int cur,int pre){
	dp[cur] = 0;
	for(int i = head[cur];i!=-1;i = edge[i].next){
		if(edge[i].to != pre){
			dfs(edge[i].to,cur);
			dp[cur] += dp[edge[i].to]+edge[i].dir;
		}
	}
}

void dfs1(int cur,int pre){
	for(int i = head[cur];i!=-1;i = edge[i].next){
		if(edge[i].to!=pre){
			int v = edge[i].to;
			dp[v] = dp[cur]+(edge[i].dir?-1:1);
			dfs1(v,cur);
		}
	}
}

int main(){
	while(scanf("%d",&n)!=EOF){
		init();
		int u,v;
		for(int i = 1;i<n;i++){
			scanf("%d%d",&u,&v);
			add(u,v,0);
			add(v,u,1);
		}
		dfs(1,0);
		dfs1(1,0);
		int minn = 0x3f3f3f3f;
		for(int i = 1;i<=n;i++){
			minn = min(minn,dp[i]);
		}
		printf("%d\n",minn);
		for(int i = 1;i<=n;i++){
			if(dp[i] == minn){
				printf("%d ",i);
			}
		}
		putchar('\n');
	}
	
	return 0;
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值