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

【CodeForces - 219D 】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 

題目大意:

 給出N個點,其中有N-1條有向邊,邊的方向可以改變,問最少改變多少條邊可以從某一個點到達任意一個點,同時求出這些點。

解題報告:

給一段一句話題解:

要改變的邊的權值為1,不需要改變的邊的權值為0,兩次dfs,第一次算出以1點為根節點到所有點要改變的邊數,第二次以1為根節點向下遍歷節點
 3 算出每一個點到達所有點要改變的邊數,dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一點的值是他父節點
 4 的值減去他以前的值再考慮他與父節點之間的邊的方向。

  其實就是兩邊dfs就好了,第一遍是正常的遞迴以cur為根的狀態值,第二遍求出轉移到cur的那一半的狀態值。也就是第一次dfs是用兒子更新父親,第二次dfs是用父親處理一下之後來更新兒子。好題啊精妙的!!

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int n;
int a,b;
ll dp[MAX];//指向我的有多少條邊 
ll ans[MAX];
struct Node {
	int to;
	bool f;//1==實邊,0==虛邊 
	Node(){}
	Node(int to,int f):to(to),f(f){}
};
vector<Node> vv[MAX];
void dfs(int cur,int root) {
	int up = vv[cur].size();
	for(int i = 0; i<up; i++) {
		Node v = vv[cur][i];
		if(v.to == root) continue;
		dfs(v.to,cur);
		dp[cur] += dp[v.to];
		if(v.f == 0) dp[cur]++; 
	}
}
void DFS(int cur,int root) {
	int up = vv[cur].size();
	for(int i = 0; i<up; i++) {
		Node v = vv[cur][i];
		if(v.to == root) continue;
		dp[v.to] += (dp[cur]-dp[v.to]);
		if(v.f) dp[v.to]++;
		else dp[v.to]--;
		DFS(v.to,cur);
	}
}
int main()
{
	cin>>n;
	for(int i = 1; i<=n-1; i++) {
		scanf("%d%d",&a,&b);
		vv[a].pb(Node(b,1));vv[b].pb(Node(a,0));
	}
	dfs(1,-1);
	DFS(1,-1);
	ll minn = 0x3f3f3f3f3f;
	for(int i = 1; i<=n; i++) minn = min(minn,dp[i]);
	printf("%lld\n",minn);
	int ans = 0;
	for(int i = 1; i<=n; i++) {
		if(minn == dp[i]) printf("%d ",i);
	}
	return 0 ;
 }

總結: 程式碼中DFS中無意間呼叫了dfs,TLE8了,,但是沒有wa,,仔細想一下確實是不會wa的,,這裡的dp不會再重新算一遍,因為葉子結點的dp永遠沒有改變過,所以不會出現多呼叫一次dfs就會使每個節點dp的值成倍的增加的現象。所以其實在這裡多呼叫一次dfs頂多算是重新求了一次dp的值,並不會使dp改變成原來的多少倍那樣、、

注意DFS的更新順序,,不然就會出錯!!總之牢牢把握住,更新狀態的前提是所用的值一定是所需的完成值就好了。