D. Choosing Capital for Treeland

链接: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 ≤ 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
  1. 3
    2 1
    2 3
output
  1. 0
    2
input
  1. 4
    1 4
    2 4
    3 4
output
  1. 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 = ;
  8. const int MAXM = ;
  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. inline int read() {
  24. int x = ,f = ;char ch = getchar();
  25. for (; ch<''||ch>''; ch = getchar())
  26. if (ch=='-') f = -;
  27. for (; ch>=''&&ch<=''; ch = getchar())
  28. x = x*+ch-'';
  29. return x*f;
  30. }
  31.  
  32. inline void add_edge(int u,int v,int w) {
  33. e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot;
  34. }
  35.  
  36. void dfs1(int u,int fa) {
  37. for (int i=head[u]; i; i=e[i].nxt) {
  38. int v = e[i].to,w = e[i].w;
  39. if (v==fa) continue;
  40. dfs1(v,u); // 叶 -> 根
  41. dps[u] += dps[v]+w;
  42. }
  43. }
  44. void dfs2(int u,int fa) {
  45. for (int i=head[u]; i; i=e[i].nxt) {
  46. int v = e[i].to,w = e[i].w;
  47. if (v==fa) continue;
  48. dpf[v] += (w?:)+dpf[u]+dps[u]-dps[v]-w;
  49. dfs2(v,u); //根 -> 叶
  50. }
  51. }
  52.  
  53. int main() {
  54.  
  55. int n = read();
  56. for (int u,v,i=; i<n; ++i) {
  57. u = read(),v = read();
  58. add_edge(u,v,),add_edge(v,u,);
  59. }
  60. dfs1(,);
  61. dfs2(,);
  62.  
  63. for (int i=; i<=n; ++i) {
  64. ans[i].v = dps[i]+dpf[i];
  65. ans[i].id = i;
  66. }
  67. sort(ans+,ans+n+);
  68.  
  69. int sum = n--ans[].v,cnt = ;
  70. for (int i=; i<=n; ++i)
  71. if (ans[i].v==ans[].v) cnt++;
  72. else break;
  73.  
  74. printf("%d\n",sum);
  75. for (int i=; i<=cnt; ++i) {
  76. printf("%d ",ans[i].id);
  77. }
  78. return ;
  79. }

CF 219 D:Choosing Capital for Treeland(树形dp)的更多相关文章

  1. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  2. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  3. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  4. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  5. [codeforces219D]Choosing Capital for Treeland树形dp

    题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...

  6. CodeForces 219D Choosing Capital for Treeland (树形DP)经典

    <题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...

  7. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  8. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)

    Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...

  9. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

随机推荐

  1. AJPFX关于JDK,JRE,JVM的区别与联系

    很多朋友可能跟我一样,对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天我们来整理下三者的关系. JDK : Java Development ToolKit(Java开发工具包) ...

  2. 新项目升级到JFinal3.5之后的改变-着重体验自动依赖注入

    最近,JFinal3.5发布,喜大普奔,我也应JBolt用户的需求,将JBolt进行了升级,实现可配置自动注入开启,支持JFinal3.5的项目生成.具体可以看:JBolt升级日志 这等工作做完后,我 ...

  3. eclipse3.4+对的处理插件(附SVN插件安装实例)

    Eclipse 3.4以前安装插件无非有两种方式, 直接copy插件到features/plugins目录或者在links目录下创建链接文件. Eclipse 3.4又推出另一种新的安装途径, 更加灵 ...

  4. Java中方法的继承以及父类未被子类覆盖的方法调用的问题

    在看java继承这一块的时候发现了一个问题,即父类未被子类覆盖的方法是如何调用的? 是子类拥有了父类的该方法只是没有显示表示,还是子类调用了父类的该方法. 为此做了一下验证 代码如下: public ...

  5. OTOH

    OTOH n 网络用语 On the Other Hand 另一方面 [例句]OTOH, pressure on the keys of a digital AFTER bottoming can b ...

  6. python - 日期处理模块

    首先就是模块的调用,很多IDE都已经安装好了很多Python经常使用到的模块,所以我们暂时不需要安装模块了. ? 1 2 3 import datetime import time import ca ...

  7. Maven各种常用架包配置文件,保存一份

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. Java基础面试操作题:Java代理工厂设计模式 ProxyFactory 有一个Baby类,有Cry行为,Baby可以配一个保姆 但是作为保姆必须遵守保姆协议:能够处理Baby类Cry的行为,如喂奶、哄睡觉。

    package com.swift; public class Baby_Baomu_ProxyFactory_Test { public static void main(String[] args ...

  9. 使用lua做序列化和反序列化

    -- lua对象序列化 function serialize(obj) local lua = "" local t = type(obj) if t == "numbe ...

  10. C++简易酒店管理系统,实现(查询、入住、退房、楼层选择、退出)功能

    #include <iostream> #include <string.h> #include <stdlib.h> void enter(); void che ...