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

D. Choosing Capital for Treeland

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 ≤ nsi ≠ 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 

题意:有n个点,n-1条单向边,如果选择其中一个点作为root,那么从这个点到其他所有点(不返回)需要改变一些单向边才能到达,问你合适的选择root点使改变的单向边数目最少,输出最少数目和这些合适的root点。

思路:由于这棵树的边是单向边,我们可以建边(u,v,0)和(v,u,1),费用为1就代表需要改变这条边方向。设up[i]为该节点到根节点需要改变的边数,down[i]表示以该节点为根的子树需要改变的边数,dp[i]表示该节点改变的单向边最少。则dp[v]=dp[u]+1(u->v的长度为0),dp[v]=dp[u]-1(u->v的长度为1);

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200005;
typedef long long ll;
int up[maxn], down[maxn], dp[maxn];
struct node
{
	int en, next,len;
}Edge[4*maxn];
int num, head[maxn]; 
bool vis[maxn];
int a[maxn];
void init()
{
	num = 0;
	memset(head, -1, sizeof(head));
	memset(Edge, 0, sizeof(Edge));
	memset(vis, 0, sizeof(vis));
	memset(dp, 0, sizeof(dp));
	memset(up, 0, sizeof(up));
	memset(down, 0, sizeof(down));
}
void add(int st, int en,int len)
{
	Edge[num].en = en;
	Edge[num].len = len;
	Edge[num].next = head[st];
	head[st] = num++;
}
void dfs1(int u)
{
	vis[u] = true;
	for (int i = head[u];i != -1;i = Edge[i].next)
	{
		int v = Edge[i].en;
		if (vis[v]) continue;
		if (!Edge[i].len) up[v] += (up[u] + 1);
		else up[v] += up[u];
		dfs1(v);
		if (Edge[i].len) down[u] += (down[v] + 1);
		else down[u] += down[v];
	}
}
void dfs2(int u)
{
	vis[u] = true;
	for (int i = head[u];i != -1;i = Edge[i].next)
	{
		int v = Edge[i].en;
		if (vis[v]) continue;
		if (Edge[i].len) dp[v] = dp[u] - 1;
		else dp[v] = dp[u] + 1;
		dfs2(v);
	}
}
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		init();
		for (int i = 1;i < n;i++)
		{
			int st, en;
			scanf("%d%d", &st, &en);
			add(st, en, 0);
			add(en, st, 1);
		}
		dfs1(1);
		memset(vis, 0, sizeof(vis));
		dp[1] = up[1] + down[1];
		dfs2(1);
		int minn = 0x7f7f7f7f;
		for (int i = 1;i <= n;i++)
			minn = min(minn, dp[i]);
		int cnt = 0;
		for (int i = 1;i <= n;i++)
			if (dp[i] == minn)
				a[cnt++] = i;
		printf("%d\n", minn);
		for (int i = 0;i < cnt;i++)
			printf("%d ", a[i]);
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值