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遍歷,這一次用自底向上的遍歷。
然後,第二層更新出來每個點作爲首都是需要改變方向的路的個數,這一次是自頂向下的遍歷,遞推關係如下:
dp[v]=dp[v]+(dp[u]dp[v])(dir[u][v]?1:1)(vu) dp[v] =dp[v]+(dp[u]-dp[v])+(dir[u][v]?1:-1)(v是u的子節點)
這地方簡單描述一下:因爲我們第二次是自頂向下遍歷,所以在更新一個子節點的時候,當前的父節點已經更新完。這個時候以子節點爲根的子樹的更改個數在第一遍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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章