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. POJA Star not a Tree?(模拟退火)

    题意 题目链接 给出$n$个点,求出一个点使得到各个点的距离之和最小,距离为欧几里得距离 Sol 模拟退火真是玄学,我退了一上午,最后把exp函数去了就A了. 后来改了改,发现是大小符号的问题.. 但 ...

  2. 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  3. shell命令cut

    cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...

  4. [VC]WindowProc和DefWindowProc函数

    在Windows操作系统里,当窗口显示之后,它就可以接收到系统源源不断地发过来的消息,然后窗口就需要处理这些消息,因此就需要一个函数来处理这些消 息.在API里定义了一个函数为回调函数,当系统需要向窗 ...

  5. cookie存验证码时间,时间没走完不能再次点击

    <script> var balanceSeconds=getcookie('Num'); console.log(balanceSeconds) var timer; var isCli ...

  6. 2018.4.9 Ubuntu install kreogist-mu

    先下载kreogist m文件 然后在下载哪里右键点击打开终端 输入sudo dpkg -i + 文件名 输入密码 下一步会显示 未安装未安装软件包 libmpv1. jiexialai要处理 sud ...

  7. 2018.4.6 java交易记录系统

    题目 ###1.交易明细文件内容如下例: 客户号 姓名 所述机构号 性别 帐号 发生时间 发生额 000001|刘德华|0000|1|4155990188888888|20060720200005|3 ...

  8. C-基础:形参char *&p与char *p

    char* &p:以引用传递的方式传指针char* p: 以值传递的方式传指针

  9. Linux连接外网

    1.给linux配置ip 进行远程管理 如果网络不同的话需要配置网卡,命令如下: vi /etc/sysconfig/network-scripts/ifcfg-enp0s3   回车 就出现网络的环 ...

  10. Object类 任何类都是object类的子类 用object对象接收数组 object类的向上向下转型

    任何类都是object类的子类 用object对象接收数组 object类的向上向下转型