Choosing Capital for Treeland(树形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 

 题意:选取某个节点,其他节点到这个节点的路径中反转的路径最小。

换成人话就是,给的单向路都是路,只不过,原来的是不要钱的,反转后要1块钱。所以添加边的时候是 add(a,b,0),add(b,a,1)意思是a到b不要钱,b到a需要1元钱。其他没连接的点不能通过。

假设我们一直1点作为定点,到其他所有点的距离。那假设1连接着2,当定点转变为2时,由于给定的是个树,这一改变所需要的代价变化仅仅产生与1-2这个边。

因此若1->2,那2作为定点的代价是dp[1] + 1。因为从2到达1,需要逆转道路;

而2->1则dp[2] = dp[1]。

2的代价求出来了,那与2连接的点的代价也能类似求出。

那怎么求1节点的代价呢?

我们只要从叶子结点往上dp直到即可。实现方式是用dfs,等搜到叶子结点,他就开始往上dp了。

完整代码:

#include
#define int long long
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
using namespace std;
const int mod = 1000000009;
const int N = 2e5 +10;

int down[N],dp[N];
int e[4*N],ne[4*N],w[4*N],h[N],idx;
bool st[N];

void add(int a,int b,int c){
	e[idx] = b; w[idx] = c; ne[idx] = h[a];h[a] = idx++;
}

void dfs1(int u){
	st[u] = true;
	for(int i = h[u];i != -1;i = ne[i]){
		int j = e[i];
		if(!st[j]){
			dfs1(j);
			// 叶子结点才会开始进行这一步,后自叶子节点往上dp
			if(w[i]) down[u] += (down[j] + 1);//我们要u->j,原本应该是j -> u。则需花1代价
			else down[u] += down[j];
		}
	}
}

void dfs2(int u){
	st[u] = true;
	for(int i = h[u];i != -1;i = ne[i]){
		int j = e[i];
		if(!st[j]){
			if(w[i]) dp[j] = dp[u] - 1;// 如果是翻转的路
			else dp[j] = dp[u] + 1;
			dfs2(j);
		}
	}
}

signed main(){
	memset(h,-1,sizeof h);
	int n; cin >> n;
	rep(i,1,n-1){
		int a,b; cin >> a >> b;
		add(a,b,0);add(b,a,1);
	}
	
	dfs1(1);
	memset(st,0,sizeof st);
	dp[1] = down[1];// 若1是根节点,所有点到这个的点需要的代价为,其实可以不用加up[1]
	
	dfs2(1);
	int minn = 1e9;
	
	rep(i,1,n) minn = min(minn,dp[i]);
	cout << minn << endl;
	rep(i,1,n) if(dp[i]==minn) cout <

你可能感兴趣的:(codeforces补题,dfs,树形dp,c++,算法,搜索)