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

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/84183401

题目:

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-1条有向边,然后在有向图中去求解:为了能够顺利到达所有点,需要修改路径方向的数目。因为是树形图求解最优决策,所以选择使用树形dp,先第一dfs求得该点能够到达的点的最大数目,第二次进行该点能到达所有点 需要修改的点的数目。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn=3e5;
int dpson[maxn],dpfather[maxn];

struct Edge{
	int u,v,w,next;
}edge[maxn<<1];
int cnt,head[maxn<<1];

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

void add_edge(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;	
}

void dfs1(int x,int pre)
{
	for(int i=head[x];i!=-1;i=edge[i].next)
	{
		if(edge[i].v==pre)
			continue;
		dfs1(edge[i].v,x);
		dpson[x]=dpson[x]+edge[i].w+dpson[edge[i].v];
	}//第一次dfs统计第一个结点需能达到的最大路数。 
}
void dfs2(int x,int pre)
{
	for(int i=head[x];i!=-1;i=edge[i].next)
	{
		if(edge[i].v==pre)
			continue;
		int w=edge[i].w;
		dpfather[edge[i].v]= (w?0:1)+dpfather[x]+dpson[x]-dpson[edge[i].v]-w;
		dfs2(edge[i].v,x);
	}
}//第二次dfs统计第一个结点达到所有结点的所需修改的路数 

struct Anss{
	int ans,i;
}anss[maxn];

bool cmp(Anss a,Anss b)
{
	return a.ans>b.ans||a.ans==b.ans&&a.i<b.i;
}
int main()
{
	int n;
	while(scanf("%d",&n)==1)
	{
		init();
		for(int i=1;i<n;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			add_edge(a,b,1);
			add_edge(b,a,0);	
		}	
		dfs1(1,-1);
		dfs2(1,-1);
		for(int i=0;i<n;i++)
		{
			anss[i].ans=dpson[i+1]+dpfather[i+1];
			anss[i].i=i+1;
		}
		sort(anss,anss+n,cmp);
		int p=anss[0].ans;
		printf("%d\n",n-1-p);
		printf("%d",anss[0].i);
		for(int i=1;i<n;i++)
			if(p==anss[i].ans)
				printf(" %d",anss[i].i);
		printf("\n");	
	}		
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/84183401
今日推荐