1. 程式人生 > >【Choosing Capital for Treeland 】【CodeForces - 219D 】(樹形dp)

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

題目:

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");	
	}		
}