Choosing Capital for Treeland (树形dp+双向搜索)
来源:互联网 发布:知乎,京九高铁 编辑:程序博客网 时间:2024/05/29 20:01
题目:Choosing Capital for Treeland
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.
Example
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条有向边,每条边的方向都可以翻转,翻转的代价是1,问从任意一个节点出发,到其他所有节点要逆向走多少条路的最小值。
思路:
题目的树是一棵有方向的树,所以对于任意一个节点,搜索向上和向下两个方向。
用0和1表示边的方向,题目就转换为了求每个点到其他点的权值问题。
设置一个dp数组。
dp[i][0]表示以i为根的子树中,从i出发,要反转多少条边
dp[i][1]表示从i往父亲方向走,要反转多少条边
这一类的题目,构造好树形结构,找父亲和孩子的关系,
向下遍历,向上更新,先dfs后dp
向下遍历,向下更新,先dp后dfs
源代码:
- Choosing Capital for Treeland (树形dp+双向搜索)
- Choosing Capital for Treeland (树形dp)
- CodeForces 219D Choosing Capital for Treeland (树形dp)
- codeforces 219D Choosing Capital for Treeland(树形DP)
- CF 219 D Choosing Capital for Treeland(树形dp)
- CodeForces 219D Choosing Capital for Treeland(树形DP)
- CF 219D Choosing Capital for Treeland (树形dp)
- CF 219D Choosing Capital for Treeland (树形dp)
- CF#219 D. Choosing Capital for Treeland(树形DP)
- 树形dp 219D Choosing Capital for Treeland
- [CF 219D]Choosing Capital for Treeland[树形DP]
- cf 219D Choosing Capital for Treeland 树形dp
- CodeForces 219D Choosing Capital for Treeland | 树形dp
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
- CodeForces 219D Choosing Capital for Treeland (树形DP)
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- CF 219D. Choosing Capital for Treeland 树形DP
- cf 135div2D Choosing Capital for Treeland 【树形dp】
- Python-matplotlib入门--基础图表的绘制
- 抽象类(abstract class)和接口(interface)有什么异同?
- 按键驱动程序
- Java开发工程师的面试该用什么姿势?做到这些让你多拿5K
- 基于Unity3D的相机功能的实现(四)——第三人称相机(TPS)
- Choosing Capital for Treeland (树形dp+双向搜索)
- Caffe编译Bug
- java 设计模式:构建器模式
- 嵌入式学习
- 算法分析与设计丨第十二周丨LeetCode(16)——Minimum Path Sum(Medium)
- 追赶法
- 机房收费系统-组合查询
- TCP和UDP区别
- Unity协程(Coroutine)原理深入剖析