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. 拖拽调整Div大小

    今天写了一天这个jquery插件: 可以实现对div进行拖拽来调整大小的功能. (function ($) { $.fn.dragDivResize = function () { var delta ...

  2. .net的基础知识点

    在这个It市场都是风云变化的,都是又市场供需来定的,当年iOS火的一塌糊涂的,现在也出现找不到工作的,满地的出入门者,我就属于其中一个,在一个逼不得已的情况下,开始转行做.net ,我相信当年的很多. ...

  3. Vue.js之vue-router路由

    vue学习的一系列,全部来自于表哥---表严肃,是我遇到过的讲课最通透,英文发音最好听的老师,想一起听课就去这里吧 https://biaoyansu.com/i/hzhj1206 1概述 vue-r ...

  4. Linux系统常用命令大全

    一.系统信息操作(备注:红色标记为常用命令,以下类推,不再赘述) arch 显示机器的处理器架构(1) uname -m   显示机器的处理器架构(2) uname -r               ...

  5. (一)我的Javascript系列:Javascript的面向对象旅程(上)

    今宵酒醒何处,杨柳岸,晓风残月 导引 我的JavaScript系列文章是我自己对JavaScript语言的感悟所撰写的系列文章.现在还没有写完.目前一共出了下面的系列: (三)我的JavaScript ...

  6. Linux之bash shell的学习

    1.什么是bash  shell bash 是Bourne Again Shell的简称,是从unix系统中的sh发展而来,是用户和偶Linux内核交互的工具,用户通过bash操作内核完成系统的使用和 ...

  7. 问题驱动的Git学习

    (搬运自我在SegmentFault的博客) 本人是个Git新手,平时用Git最多的就是push,因为别的都不怎么会用.这几天因为在小组中负责代码的整合,顺便将代码提交到Github,接触到了Git更 ...

  8. jvm | 基于栈的解释器执行过程

    一段简单的算术代码: public class Demo { public static void main(String[] args) { int a = 1; int b = 2; int c ...

  9. javaweb基础(13)_session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  10. Maven搭建Struts2+Spring3+Hibernate4框架

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...