CF 219D Choosing Capital for Treeland(树形DP)

题目链接:http://codeforces.com/problemset/problem/219/D

题目:

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
inputCopy
3
2 1
2 3
outputCopy
0
2
inputCopy
4
1 4
2 4
3 4
outputCopy
2
1 2 3

题意:给定一棵有向树。要使得从一个点出发到达所有的点,边可以翻转(u — v  翻转成v — u),问最少翻转次数为多少,输出所有最少翻转次数时的点。

题解:跑两次DFS,第一次DFS,以顶点1为根,跑下 往下的最少翻转次数(由儿子节点转移过来)。再一次DFS,跑下。更新儿子节点需要的最少翻转次数。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N=2e5+10;
 5 const int INF=0x3f3f3f3f;
 6 vector < pair<int,int> > E[N];
 7 int dp[N];
 8 
 9 void DFS1(int u,int Fa){
10     dp[u]=0;
11     for(int i=0;i<E[u].size();i++){
12         int v=E[u][i].first;
13         int w=E[u][i].second;
14         if(v==Fa) continue;
15         DFS1(v,u);
16         dp[u]+=(dp[v]+w);
17     }
18 }
19 
20 void DFS2(int u,int Fa){
21     for(int i=0;i<E[u].size();i++){
22         int v=E[u][i].first;
23         int w=E[u][i].second;
24         if(v==Fa) continue;
25         if(w==1) dp[v]=dp[u]-1;
26         else dp[v]=dp[u]+1;
27         DFS2(v,u);
28     }
29 }
30 
31 int main(){
32     int n;
33     scanf("%d",&n);
34     for(int i=1;i<n;i++){
35         int u,v;
36         scanf("%d%d",&u,&v);
37         E[u].push_back(make_pair(v,0));
38         E[v].push_back(make_pair(u,1));
39     }
40     DFS1(1,0);
41     DFS2(1,0);
42     int ans=INF;
43     for(int i=1;i<=n;i++) ans=min(ans,dp[i]);
44     printf("%d\n",ans);
45     for(int i=1;i<=n;i++){
46         if(dp[i]==ans) printf("%d ",i);
47     }
48     return 0;
49 }
扫描二维码关注公众号,回复: 44475 查看本文章

猜你喜欢

转载自www.cnblogs.com/Leonard-/p/8909729.html