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
32 12 3

Output
02 

Input
41 42 43 4

Output
21 2 3
题解:将方向转化为边的权值,正向边权值为0,反向边权值为1.以1为根DFS,得到1到其它所有点的总花费。重点是第二次DFS:
       

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=2e5+5;
 5
 6 struct node{
 7     int to,next,va;
 8 }e[2*maxn];
 9
10 int n,tot;
11 int dp[maxn],head[maxn];
12
13 void Inite(){
14     tot=0;
15     memset(head,-1,sizeof(head));
16 }
17
18 void addedge(int u,int v,int w){
19     e[tot].to=v;
20     e[tot].va=w;
21     e[tot].next=head[u];
22     head[u]=tot++;
23 }
24
25 void DFS1(int pa,int u){
26     for(int i=head[u];i!=-1;i=e[i].next){
27         int v=e[i].to;
28         if(pa==v) continue;
29         DFS1(u,v);
30         dp[u]+=dp[v]+e[i].va;
31     }
32 }
33
34 void DFS2(int pa,int u){
35     for(int i=head[u];i!=-1;i=e[i].next){
36         int v=e[i].to;
37         if(pa==v) continue;
38         dp[v]+=(dp[u]-dp[v])+((e[i].va)?-1:1);
39         DFS2(u,v);
40     }
41 }
42
43 int main()
44 {   Inite();
45     memset(dp,0,sizeof(dp));
46
47     scanf("%d",&n);
48     for(int i=2;i<=n;i++){
49         int u,v;
50         scanf("%d%d",&u,&v);
51         addedge(u,v,0);
52         addedge(v,u,1);
53     }
54
55     DFS1(0,1);
56     DFS2(0,1);
57
58     int temp=1e9;
59     vector<int> q;
60     for(int i=1;i<=n;i++) if(dp[i]<temp) temp=dp[i];
61     for(int i=1;i<=n;i++) if(dp[i]==temp) q.push_back(i);
62     printf("%d\n",temp);
63     for(int i=0;i<q.size();i++) printf("%d%c",q[i],(i==(q.size()-1))?'\n':' ');
64
65 }

转载于:https://www.cnblogs.com/zgglj-com/p/7765972.html

Choosing Capital for Treeland codeforce 219-D相关推荐

  1. Codeforces - Choosing Capital for Treeland

    题目链接:Codeforces - Choosing Capital for Treeland 显然,如果确定首都之后,我们可以O(n)计算出这个点的答案. 然后这个东西可以换根吗?显然是可以的.换根 ...

  2. 【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)

    题干: The country Treeland consists of n cities, some pairs of them are connected with unidirectional  ...

  3. 树形DP——Codeforces Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题意:给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v&g ...

  4. oracle中having作用,oracle中having与where的区别

    1.where 不能放在group by 的后面 2.HAVING 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE 3.WHERE 后面的条件中不能有聚集函 ...

  5. 2019 HZNU Winter Training Day 14 Comprehensive Training

    A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...

  6. 【缄*默】 #DP# 各种DP的实现方法(更新ing)

    DP =「状态」+「阶段」+「决策」 基本原理 = 「有向无环图」+「最优子结构」+「无后效性」 目录 一. 线性DP { 1.概念引入 } { 2.例题详解 } [例题1]caioj 1064 最长 ...

  7. HTML 中的字符实体集

    ================== 数字与特殊图形实体 ================== < < 左尖括号 > > 右尖括号 & & 连接符 " ...

  8. markdown、html转义特殊字符代码大全

    markdown.html转义特殊字符代码大全 注意: 使用数字代码或英文代码需要在代码后面加一个英文的分号「;」 第二列转义符号为使用数字代码或英文代码后得的的符号 原符号 转义符号 数字代码 英文 ...

  9. HTML转义特殊字符字符

    学习内容:HTML转义字符大全 java过滤器过滤危险字符 转义:% & @ ! ^ & * ( ) _ + ISO Latin-1字符集 //制表符Horizontal tab // ...

  10. Codeforce 106B Choosing Laptop

    题意: V想买电脑,花最小的钱,且不是从性能的三个方面都被其他任意一个电脑全部压制的电脑的编号. 代码: #include<bits/stdc++.h> using namespace s ...

最新文章

  1. JavaScript 函数参数
  2. 2.羽翼sqlmap学习笔记之MySQL注入
  3. 介绍Python的魔术方法 - Magic Method
  4. 电气工程师学python_Python 在电气工程及其自动化上有哪些应用?
  5. mybatis 执行插入操作,insert 返回1,数据库中无数据。数据库中数据的创建时间和插入执行时间不一致。
  6. 再看lambda/sorted/filter/map
  7. Kotlin 1.2 有哪些新特性
  8. Oracle批量插入数据
  9. 台大李宏毅Machine Learning 2017Fall学习笔记 (12)Why Deep?
  10. ASP.NET Page执行顺序如:OnPreInit()、OnInit()(转)
  11. H3C基础配置命令(一)
  12. IDW空间插值法matlab,基于IDW对PM2.5进行空间插值及可视化
  13. VUE调用打印机打印页面
  14. android小米手机变慢,手机越来越慢怎么破?小米手机七大加速绝招
  15. html中快速复制上一行并粘贴到下一行
  16. 第二证券|扶持政策频发,教育板块再度爆发,全通教育“20cm”涨停
  17. ssh密钥-帮助文档
  18. java线程堆栈nid.tid_java排查一个线上死循环cpu暴涨的过程分析
  19. Linux运维18:网络和监控
  20. 计算机硬件故障的相关知识,计算机硬件故障【计算机硬件故障的判断与维修】...

热门文章

  1. 【渝粤教育】电大中专微生物与寄生虫基础_1作业 题库
  2. 手把手教你强化学习 (九) 强化学习中策略梯度算法
  3. 矩阵分解SVD在推荐系统中的应用
  4. swift菜鸟入门视频教程-02-基本运算符
  5. java.lang.NoClassDefFoundError: org/apache/log4j/Priority的问题解决
  6. docker导入导出
  7. 从源码分析非线程安全集合类的不安全迭代器
  8. C/C++ 进程间通信 管道
  9. 【Asp.Net Core】在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
  10. [转]适合过一辈子的人