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
3
2 1
2 3
output
0
2
input
4
1 4
2 4
3 4
output
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

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int MAXN = ;
const int MAXM = ; struct Edge{
int to,nxt,w;
}e[MAXM];
struct ANS{
int id,v;
bool operator < (const ANS &a) const {
if (v==a.v) return id < a.id;
return v > a.v;
}
}ans[MAXN];
int head[MAXM],tot;
int dps[MAXN],dpf[MAXN]; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar())
x = x*+ch-'';
return x*f;
} inline void add_edge(int u,int v,int w) {
e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot;
} void dfs1(int u,int fa) {
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==fa) continue;
dfs1(v,u); // 叶 -> 根
dps[u] += dps[v]+w;
}
}
void dfs2(int u,int fa) {
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==fa) continue;
dpf[v] += (w?:)+dpf[u]+dps[u]-dps[v]-w;
dfs2(v,u); //根 -> 叶
}
} int main() { int n = read();
for (int u,v,i=; i<n; ++i) {
u = read(),v = read();
add_edge(u,v,),add_edge(v,u,);
}
dfs1(,);
dfs2(,); for (int i=; i<=n; ++i) {
ans[i].v = dps[i]+dpf[i];
ans[i].id = i;
}
sort(ans+,ans+n+); int sum = n--ans[].v,cnt = ;
for (int i=; i<=n; ++i)
if (ans[i].v==ans[].v) cnt++;
else break; printf("%d\n",sum);
for (int i=; i<=cnt; ++i) {
printf("%d ",ans[i].id);
}
return ;
}

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. 从一个LocalDateTime引发的疑问

    一 公司有同事部署出错,然后查日志,找时间,从k8s得到的时间是  2017-06-16T09:38:48.580 +0000,然后他就纳闷了,因为他根本不会在9点部署好吧,而且9点大多数程序员都没开 ...

  2. paas相关,添加ing

    1. docker 构建镜像,docker build -t image_name:version dockerfilePath.使用镜像启动一个docker容器,docker run --name ...

  3. AJPFX浅析Java数组

    数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信息分组的便利方法.注意:如 ...

  4. Spring RestTemplate实现服务间的远程调用完整代码示例

    父pom: 服务提供方 pom: provider配置文件: provider启动类: provider实体类: provider Mapper: 内置了增删改查的方法 provider Servic ...

  5. ubuntu server 16.04安装GPU服务器

    1 Ubuntu16.04 系统安装过程中,需要勾选openssh-server 方便远程连接 2 必须安装gcc 与g++ 3 安装显卡驱动 NVIDIA-Linux-x86_64-367.57.r ...

  6. Unity3d通过脚本生成apk

    参考链接:http://www.jianshu.com/p/a9261113b4ac 照着链接的方法并没有正确生成APK,IPA没有测试过,不过大致的方法是正确的,修改如下: Environment. ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. 如何处理错误消息Please install the Linux kernel header files

    Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇 ...

  9. Mongodb之failed to create service entry worker thread

    Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...

  10. uoj#300.【CTSC2017】吉夫特

    题面:http://uoj.ac/problem/300 一道大水题,然而我并不知道$lucas$定理的推论.. $\binom{n}{m}$为奇数的充要条件是$n&m=n$.那么我们对于每个 ...