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. 老技术,UrlRewriter实现全站伪静态

    看人家做淘宝客很火,就做了个网站.seo的话当然需要全站伪静态了,问了下空间商不支持mvc,尼玛,好吧,isapi_rewrite支持吗?“额,不支持!” -_-! 额,好吧,搬出n年前的东西了:微软 ...

  2. 解决mysql本地数据库不能用ip访问的问题

    [转]http://gone18611.blog.163.com/blog/static/1851943120104705244116/ MYSQL数据库缺省安装后,其默认用户名ROOT如果只能以&l ...

  3. 第一课:K线

    1       K线是根据价格走势中形成的四个价位(开盘价.收盘价.最高价.最低价)绘制而成的.K线是最基本的描述股价涨跌的表现符号(记录某种股票一天的价格变动情况). K线构造的四个价格因素:开盘价 ...

  4. sublime text 快捷键新建.vue

    第一步:添加模板: 模板写法如下: <template> </template> <script type="ecmascript-6"> &l ...

  5. SharePoint Server和Office 365之间的混合模式集成概述

    正如您可能已经知道的那样,云中的Microsoft Office 365和SharePoint Server 2013/2016内部部署可以通过多种方式协同工作.这些通常被称为混合模式,因为它们将功能 ...

  6. 重写strcpy函数,以实现strcpy的功能

    char * strcpyTest(char *dst,const char *src);Action(){ char *ptr=(char*)malloc(100); char a[]={" ...

  7. Linux Shell流程例子

    #!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...

  8. 有一个无效 SelectedValue,因为它不在项目列表中

    “Drp_XX”有一个无效 SelectedValue,因为它不在项目列表中 出现以上异常的原因肯定是将DrowDownList控件的SelectedValue属性赋值为一个列表中不存在的值.那么我们 ...

  9. python_98_面向对象_学校

    class School(object):#以后都加object(基类) def __init__(self, name, addr): self.name = name self.addr = ad ...

  10. SC || Chapter6 复习向 面向可维护性 我哭了

    高内聚低耦合 高内聚:一个模块内部各个元素彼此结合的紧密程度,一个软件模块是由相关性很强的代码组成,只负责一项任务,也就是常说的单一责任原则 低耦合:各模块间相互联系紧密程度,模块间接口的复杂性.调用 ...