Choosing Capital for Treeland

Description

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.

Sample Input

Input
32 12 3

Output
02 

Input
41 42 43 4

Output
21 2 3 

【题意】

  给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点。

【分析】

  啊啊啊看题意的时候不小心看到题解了。

  啊啊啊我是不是很傻没有1秒钟看出来,数学老师说要一秒钟看出来的啊。

  嗯。。先随便找一个点作为根,计算它的值,具体怎么计算呢,就是不指向它的边要加1。(一个dfs搞定)

  然后从这个根往下走,往下走一步其实只有他们中间的边要方向,判断一下就好了。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Maxn 200010
 8 #define INF 0xfffffff
 9
10 struct node
11 {
12     int x,y,c,next;
13 }t[2*Maxn];int len=0;
14 int first[Maxn];
15
16 void ins(int x,int y,int c)
17 {
18     t[++len].x=x;t[len].y=y;t[len].c=c;
19     t[len].next=first[x];first[x]=len;
20 }
21
22 int ans=INF;
23 int op[Maxn],pl=0;
24 int d[Maxn];
25
26 void dfs(int x,int f)
27 {
28     d[x]=0;
29     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
30     {
31         int y=t[i].y;
32         dfs(y,x);
33         d[x]+=d[y]+t[i].c;
34     }
35 }
36
37 void ffind(int x,int f)
38 {
39     if(d[x]<ans)
40     {
41         pl=0;
42         op[++pl]=x;ans=d[x];
43     }
44     else if(d[x]==ans) op[++pl]=x;
45     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
46     {
47         int y=t[i].y;
48         if(t[i].c==1) d[y]=d[x]-1;
49         else d[y]=d[x]+1;
50         ffind(y,x);
51     }
52 }
53
54 int main()
55 {
56     int n;
57     scanf("%d",&n);
58     for(int i=1;i<n;i++)
59     {
60         int x,y;
61         scanf("%d%d",&x,&y);
62         ins(x,y,0);ins(y,x,1);
63     }
64     dfs(1,0);
65     ffind(1,0);
66     sort(op+1,op+1+pl);
67     printf("%d\n",ans);
68     for(int i=1;i<=pl;i++) printf("%d ",op[i]);
69     printf("\n");
70     return 0;
71 }

[CF 219D]

2016-10-17 15:04:38

转载于:https://www.cnblogs.com/Konjakmoyu/p/5969816.html

【codeforce 219D】 Choosing Capital for Treeland (树形DP)相关推荐

  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. Codeforces 1088E Ehab and a component choosing problem(树形DP)

    Codeforces 1088E Ehab and a component choosing problem(树形DP) 题意 给一棵树,要求从中选一些联通分量,使得平均联通分量重量总和最大.如果有多 ...

  5. CodeForces - 1088E Ehab and a component choosing problem(树形dp)

    题目链接:点击查看 题目大意:给出一棵树,每个顶点都有权值,在树上选出k个相互独立的连通块,使得其权值和的平均值最大的情况下选的块数最多 题目分析:这个题目中平均值的优先级大于块数,那么我们可以在树上 ...

  6. 树形dp+树形结构总结

    总结 最近写了好多树形dp+树形结构的题目,这些题目变化多样能与多种算法结合,但还是有好多规律可以找的. 先说总的规律吧! 一般来说树形dp在设状态转移方程时都可以用f[i][]表示i这颗子树怎么怎么 ...

  7. BNUOJ 52305 Around the World 树形dp

    题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52305 Around the World Time Limit: 20000msMemory ...

  8. [树形dp] Jzoj P5233 概率博弈

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵n个点的以1为根的有根树,叶子有权值.假设有m个叶子,那么树上每个叶子的权值序列就是一个1->m 的排列. 一开始在1号点有 ...

  9. fwt优化+树形DP HDU 5909

    1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include ...

  10. BZOJ 1040 ZJOI2008 骑士 树形DP

    题目大意:给定一个基环树林,每一个点上有权值,要求选择一个权值和最大的点集,要求点集中的随意两个点之间不能直接相连 最大点独立集--考虑到n<=100W,网络流铁定跑不了,于是我们考虑树形DP ...

最新文章

  1. 2014年个人工作总结
  2. 关于软件组织培训的几个值得提倡的建议
  3. 帕斯卡命名法 pascal命名法
  4. LeetCode 315. 计算右侧小于当前元素的个数(二叉查找树二分查找归并排序逆序数总结)
  5. python中的列表和元组_百度资讯搜索_python中的列表和元组
  6. const、extern、static的使用不再神秘
  7. 《细胞》重磅成果!任兵课题组绘制迄今最大规模人类单细胞染色质可及性图谱...
  8. 卷积神经网络CNN是靠什么线索学习到深度信息的?
  9. 华为鸿蒙系统学习笔记4-方舟编译器源码下载及安装
  10. mysql_connect和mysql_pconnect区别
  11. 阿里服务器降温系统,双十一服务器靠“泡澡”降温?阿里看上了3M的这项“冷”科技...
  12. 如何给自定义控件添加自定义属性
  13. 银联在线支付B2C UnionPay.NET
  14. cadlisp点选面积标注_一个在CAD中标注坐标的LISP
  15. 运筹学_单纯形法_week3
  16. python调用nmap_Python调用nmap扫描网段主机信息生成xml
  17. JAVA小白的学习总结第四周
  18. 答题对战方案java_使用WebSocket实现实时多人答题对战游戏
  19. 复利单利计算的源代码
  20. 你在日常的测试工作中遇到过哪些困境呢?

热门文章

  1. python注册登录代码_python基础--注册和登录功能 代码
  2. 技术记录 -- 只要开始,永远不晚
  3. 网页长截图工具_Mac系统如何轻松实现网页长截图功能
  4. (第二部)程序员逆天改命之胜天半子
  5. java关于map用来筛选的用法
  6. 如何在C#中生成与PHP一样的MD5 Hash Code
  7. 2013华为校园招聘机试题9月10日题(杭州)
  8. 推荐8个很酷很有用的 HTML5 应用程序
  9. python-jieba-分词----官方文档截取
  10. 第四季-专题8-LINUX系统调用