D. Choosing Capital for Treeland
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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条有向边,边的方向可以改变,问最少改变多少条边可以从某一个点到达任意一个点,同时求出这些点。

代码:

 /*
要改变的边的权值为1,不需要改变的边的权值为0,两次dfs,第一次算出以1点为根节点到所有点要改变的边数,第二次以1为根节点向下遍历节点
算出每一个点到达所有点要改变的边数,dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一点的值是他父节点
的值减去他以前的值再考虑他与父节点之间的边的方向。
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n,lne;
int dp[];
int head[];
int city[];
struct node
{
int to,next,val;
}tree[];
void add(int a,int b)
{
tree[lne].to=b;
tree[lne].val=;
tree[lne].next=head[a];
head[a]=lne++;
tree[lne].to=a;
tree[lne].val=;
tree[lne].next=head[b];
head[b]=lne++;
}
void dfs1(int root,int pre)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int son=tree[i].to;
if(son==pre)
continue;
dfs1(son,root);
dp[root]+=dp[son]+tree[i].val;
}
}
void dfs2(int root,int pre)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int son=tree[i].to;
if(son==pre)
continue;
dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-:);
dfs2(son,root);
}
}
int main()
{
int a,b;
while(scanf("%d",&n)!=EOF)
{
lne=;
memset(head,-,sizeof(head));
for(int i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
int k=,sum=;
memset(dp,,sizeof(dp));
dfs1(,);
dfs2(,);
for(int i=;i<=n;i++)
{
if(dp[i]<sum)
{
memset(city,,sizeof(city));
k=;
city[k++]=i;
sum=dp[i];
}
else if(dp[i]==sum)
city[k++]=i;
}
printf("%d\n",sum);
for(int i=;i<k;i++)
{
if(i==k-)
printf("%d\n",city[i]);
else printf("%d ",city[i]);
}
}
return ;
}

CodeForces 219D 树形DP的更多相关文章

  1. CF 219D 树形DP

    CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...

  2. Codeforces 1153D 树形DP

    题意:有一个游戏,规则如下:每个点有一个标号,为max或min, max是指这个点的值是所有子节点中值最大的那一个,min同理.问如何给这颗树的叶子节点赋值,可以让这棵树的根节点值最大. 思路:很明显 ...

  3. Codeforces 1088E 树形dp+思维

    比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...

  4. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  5. CodeForces - 337D 树形dp

    题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...

  6. codeforces 337D 树形DP Book of Evil

    原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...

  7. Up and Down the Tree CodeForces - 1065F (树形dp)

    链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...

  8. codeforces 1053D 树形DP

    题意:给一颗树,1为根节点,有两种节点,min或者max,min节点的值是它的子节点的值中最小的,max节点的值是它的子节点的值中最大的,若共有k个叶子,叶子的值依次为1~k. 问给每个叶子的值赋为几 ...

  9. Codeforces 1120D (树形DP 或 最小生成树)

    题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...

随机推荐

  1. wpf 双击行。。获得行信息

    void mydataGird_MouseDoubleClick(object sender, MouseButtonEventArgs e) { Point aP = e.GetPosition(m ...

  2. MATLAB中stem函数用法

    stem(Y) 将数据序列Y从x轴到数据值按照茎状形式画出,以圆圈终止.如果Y是一个矩阵,则将其每一列按照分隔方式画出. stem(X,Y)在X的指定点处画出数据序列Y.  stem(...,'fil ...

  3. mysql注入小测试

    转自:http://www.jb51.net/article/46163.htm 在开发网站的时候,出于安全考虑,需要过滤从页面传递过来的字符.通常,用户可以通过以下接口调用数据库的内容:URL地址栏 ...

  4. phpstudy配置ssl

    https://yunpan.cn/cPEyzVycbkiE3 (提取码:03aa) 1.重写规则:http://www.cnphp.info/htaccess-rewrite.html 2.相关文档 ...

  5. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  6. flv文件格式解析!!!

    flv头 FLV header 总体上看,FLV包括文件头(File Header)和文件体(File Body)两部分,其中文件体由一系列的Tag组成. Signature: FLV 文件的前3个字 ...

  7. prefixfree.js_无前缀脚本

    prefixfree.js是JavaScript工具库,怎么用呢... 回答就是:哼!省去CSS3中的前缀“-moz”.“-webkit”.“-o”.“-ms” 省的在写一大堆前缀来适应旧版本浏览器或 ...

  8. RabbitMQ常用命令

    创建一个用户为mytest,密码为mytest rabbitmqctl add_user mytest mytest 删除一个用户  rabbitmqctl delete_user username ...

  9. Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

    Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...

  10. Post请求

    写在前面的话: XMLHttpRequest对象的open方法的第一个参数为request-type,取值可以为get或post.本篇介绍post请求. 使用post方式时,浏览器会把各表单中字段元素 ...