【CodeForces 219D】Choosing Capital for Treeland(树形dp)

版权声明:如果有什么问题,欢迎大家指出,希望能和大家一起讨论、共同进步。 https://blog.csdn.net/QQ_774682/article/details/84145792

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

扫描二维码关注公众号,回复: 4267869 查看本文章
4
1 4
2 4
3 4

Output

2
1 2 3 

思路:

思路:

任选一个点作为根节点,dfs搜一遍,找到根节点需要转方向的边有多少,然后再来dfs从上往下推,每到一层就推出这一层的结果。例如根节点是1号它的子节点之一是2号,那么1号和2号在2这可子树上需要逆向的边数是相同的,因为这一边的信息是2传给1的。再看1号结点连的其他子树(除2号这一支之外),当以2号为根节点时,2号会扫到1号,然后会扫1号的子节点,这样1号和2号扫1号的子节点是相同的操作,因此 两者得到的结果是相同的。这样来说两者数量的差别就体现在两者之间的边上。如果1扫到2时正向的,那么2到1就是逆向的,2的逆向边比1多,所以dp[2]=dp[1]+1;反之会比1少一条。因此我们可以借助父节点与子节点之间的关系,从选定的根节点一直推到叶子节点。 需要用到两次dfs

ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
const int MAXN=2e6+100;
struct node{
	int x,nex,w;
}side[MAXN*2];//加双向边一定记得开两倍的数组 
int n,u,v;
int head[MAXN],cnt=0;
int num[MAXN],dp[MAXN];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
	memset(dp,0,sizeof(dp));
	memset(num,0,sizeof(num));
}
void add(int x,int y,int w)
{
	side[cnt].x=y;
	side[cnt].nex=head[x];
	side[cnt].w=w;
	head[x]=cnt++;
	
}

void dfs(int x,int f)
{
	
	for(int i=head[x];i!=-1;i=side[i].nex)
	{
		int ty=side[i].x;
		if(ty==f)
		continue;
		dfs(ty,x);
		num[x]+=num[ty];
		if(!side[i].w)
		num[x]++;
	}
	dp[x]=num[x];
}
void df(int x,int f)
{
	for(int i=head[x];i!=-1;i=side[i].nex)
	{
		int ty=side[i].x;
		if(f==ty)//要判断这一步否则会陷入死循环 
		continue; 
		if(!side[i].w)
		dp[ty]=dp[x]-1;
		else
		{
			dp[ty]=dp[x]+1;
		}
		df(ty,x);
	}
}
int main()
{
	init();
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v,1);//1表示边原来是正向的(标准方向) 
		add(v,u,0);// 0表示不是标准方向 
	}
		dfs(1,-1);
		df(1,-1);
	int ans=dp[1];
	int tot=1,ot[MAXN];
	ot[0]=1;
	for(int i=2;i<=n;i++)
	{
		if(ans>dp[i])
		{
			ans=dp[i];
			ot[0]=i;
			tot=1;
		}
		else if(ans==dp[i])
		{
			ot[tot++]=i;
		}
	}
	printf("%d\n",ans);
	for(int i=0;i<tot;i++)
	{
		printf("%d",ot[i]);
		if(i==tot-1)
		printf("\n");
		else
		printf(" ");
	}
	
	return 0;
}

看了别人的思路感觉这样想也挺清晰的,感觉这种转换的思想挺好的,以后在题目中可能会用到这种思想,借鉴一下

思路:

把边的方向化为权值,正向为1,逆向为0。

问题转化为找哪些点的在遍历全图后总权值最大。

这就是树形DP了,考虑每个节点,它可以从子树收获价值,也可以从父亲收获。所以dfs两遍,一边把子树的价值存到dps[i]里,再一遍把父亲的价值存到dpf[i]里。ans[i] = dps[i] + dpf[i]。

原文链接:https://blog.csdn.net/angon823/article/details/52316220 

猜你喜欢

转载自blog.csdn.net/QQ_774682/article/details/84145792