CF 219 D: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 ≤ 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
3
2 1
2 3
0
2
4
1 4
2 4
3 4
2
1 2 3
分析
题意:给一个n节点的有向无环图,要找一个这样的点:
该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)
如果有多个这样的点,则升序输出所有
思路:把边的方向化为权值,正向为1,逆向为0。
问题转化为找哪些点的在遍历全图后总权值最大。
这就是树形DP了,考虑每个节点,它可以从子树收获价值,也可以从父亲收获。
所以dfs两遍,一边把子树的价值存到dps[i]里,再一遍把父亲的价值存到dpf[i]里。
ans[i] = dps[i] + dpf[i]。
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 5 using namespace std; 6 7 const int MAXN = 200010; 8 const int MAXM = 500010; 9 10 struct Edge{ 11 int to,nxt,w; 12 }e[MAXM]; 13 struct ANS{ 14 int id,v; 15 bool operator < (const ANS &a) const { 16 if (v==a.v) return id < a.id; 17 return v > a.v; 18 } 19 }ans[MAXN]; 20 int head[MAXM],tot; 21 int dps[MAXN],dpf[MAXN]; 22 23 24 inline int read() { 25 int x = 0,f = 1;char ch = getchar(); 26 for (; ch<'0'||ch>'9'; ch = getchar()) 27 if (ch=='-') f = -1; 28 for (; ch>='0'&&ch<='9'; ch = getchar()) 29 x = x*10+ch-'0'; 30 return x*f; 31 } 32 33 inline void add_edge(int u,int v,int w) { 34 e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot; 35 } 36 37 void dfs1(int u,int fa) { 38 for (int i=head[u]; i; i=e[i].nxt) { 39 int v = e[i].to,w = e[i].w; 40 if (v==fa) continue; 41 dfs1(v,u); // 叶 -> 根 42 dps[u] += dps[v]+w; 43 } 44 } 45 void dfs2(int u,int fa) { 46 for (int i=head[u]; i; i=e[i].nxt) { 47 int v = e[i].to,w = e[i].w; 48 if (v==fa) continue; 49 dpf[v] += (w?0:1)+dpf[u]+dps[u]-dps[v]-w; 50 dfs2(v,u); //根 -> 叶 51 } 52 } 53 54 int main() { 55 56 int n = read(); 57 for (int u,v,i=1; i<n; ++i) { 58 u = read(),v = read(); 59 add_edge(u,v,1),add_edge(v,u,0); 60 } 61 dfs1(1,0); 62 dfs2(1,0); 63 64 for (int i=1; i<=n; ++i) { 65 ans[i].v = dps[i]+dpf[i]; 66 ans[i].id = i; 67 } 68 sort(ans+1,ans+n+1); 69 70 int sum = n-1-ans[1].v,cnt = 1; 71 for (int i=2; i<=n; ++i) 72 if (ans[i].v==ans[1].v) cnt++; 73 else break; 74 75 printf("%d\n",sum); 76 for (int i=1; i<=cnt; ++i) { 77 printf("%d ",ans[i].id); 78 } 79 return 0; 80 }