1. 题意翻译
  2. 题目描述
  3. Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。
  4. 输入输出格式
  5. 输入格式
  6. 输入包含多个测试点。对于每个测试点,每个测试点的第一行为一个正整数n2<=n<=2e5)。接下来n-1行,每行两个正整数aibi,表示城市a到城市b有一条单向通行的道路。输入以空格分隔,以EOF结尾。
  7. 输出格式
  8. 对于每个测试点,第一行输出k,第二行升序输出所有满足条件的首都的编号。
  9. 题目描述
  10. The country Treeland consists of n n cities, some pairs of them are connected with unidirectional roads. Overall there are n-1 n1 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.
  11. 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 a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a a to any other city. For that some roads may have to be inversed.
  12. Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
  13. 输入输出格式
  14. 输入格式:
  15. The first input line contains integer n n ( 2<=n<=2·10^{5} 2<=n<=2⋅10
  16. 5
  17. ) — the number of cities in Treeland. Next n-1 n−1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers s_{i},t_{i} s
  18. i
  19. ​ ,t
  20. i
  21. ​ ( $ 1<=s_{i},t_{i}<=n; s_{i}≠t_{i} $ ) — the numbers of cities, connected by that road. The i i -th road is oriented from city s_{i} s
  22. i
  23. ​ to city t_{i} t
  24. i
  25. ​ . You can consider cities in Treeland indexed from 1 to n n .
  26. 输出格式:
  27. 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.
  28. 输入输出样例
  29. 输入样例#1:
  30. 3
  31. 2 1
  32. 2 3
  33. 输出样例#1:
  34. 0
  35. 2
  36. 输入样例#2:
  37. 4
  38. 1 4
  39. 2 4
  40. 3 4
  41. 输出样例#2:
  42. 2
  43. 1 2 3

树形DP,先假设一个根1,然后求出每个子树里反边个数,再从上往下更新,考虑祖先的边。

min最好拿出来求,放在dfs里会导致求不到1号点。

  1. //Stay foolish,stay hungry,stay young,stay simple
  2. #include<iostream>
  3. using namespace std;
  4. const int MAXN=400005;
  5. int n;
  6. struct Edge{
  7. int next,to,w;
  8. }e[MAXN];
  9. int ecnt,head[MAXN];
  10. inline void add(int x,int y,int w){
  11. e[++ecnt].next = head[x];
  12. e[ecnt].to = y;
  13. e[ecnt].w = w;
  14. head[x] = ecnt;
  15. }
  16. int dis[MAXN];
  17. int mn=1<<30;
  18. int dfs(int now,int pre){
  19. int ret=0;
  20. for(int i=head[now];i;i=e[i].next){
  21. int v=e[i].to;
  22. if(pre==v) continue;
  23. ret+=dfs(v,now)+e[i].w;
  24. }
  25. return dis[now]=ret;
  26. }
  27. void dfs2(int now,int pre){
  28. for(int i=head[now];i;i=e[i].next){
  29. int v=e[i].to;
  30. if(v==pre) continue;
  31. dis[v]=dis[now]+(e[i].w?-1:1);
  32. // mn=min(mn,dis[v]);
  33. dfs2(v,now);
  34. }
  35. }
  36. int main(){
  37. cin.sync_with_stdio(false);
  38. cin.tie(0);
  39. cin>>n;
  40. for(int i=1;i<=n-1;i++){
  41. int x,y;
  42. cin>>x>>y;
  43. add(x,y,0);
  44. add(y,x,1);
  45. }
  46. dfs(1,-1);
  47. dfs2(1,-1);
  48. for(int i=1;i<=n;i++)
  49. mn=min(mn,dis[i]);
  50. cout<<mn<<endl;
  51. for(int i=1;i<=n;i++)
  52. if(dis[i]==mn) cout<<i<<" ";
  53. return 0;
  54. }

[CF] 219D Choosing Capital for Treeland的更多相关文章

  1. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  2. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  3. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...

  4. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  5. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  6. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

  7. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  8. Codeforces 219D Choosing Capital for Treeland 2次DP

    //选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...

  9. Codeforces 219D Choosing Capital for Treeland:Tree dp

    题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...

随机推荐

  1. lightoj 1125【01背包变性】

    题意: 从n个数里选出m个来,还要使得这m个数之和被d整除. 给一个n和q,再给n个数,再给q个询问,每个询问包含两个数,d,m; 对于每个case输出每个q个询问的可行的方案数. 思路: 每个数只能 ...

  2. 基于FBX SDK的FBX模型解析与加载 -(四)

    8. 骨骼蒙皮动画 骨骼蒙皮动画是当前游戏引擎中最常用的一种动画方式,关于其基本原理网络上的资料较多,关于到涉及的其它较复杂操作,如插值.融合等在这里也就先不再讨论了,而且其实现方式也与具体引擎的动作 ...

  3. Cg(C for Graphic)语言表达式与控制语句(转)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 在上一章中,我们已经介绍了 Cg 语言的 ...

  4. Unity插值函数Lerp()与增量时间Time.deltatime

    一.Unity插值函数Lerp() 通过官方文档简单了解插值函数(https://docs.unity3d.com/ScriptReference/index.html),可以看到插值函数有很多 Ma ...

  5. Swift @objcMembers

    使用@objcMembers关键字,将类中的所有方法暴露给Objc (效果等同于为所有方法加上@objc). 示例代码: @objcMembers class MyController: UIView ...

  6. AtCoder Grand Contest 012 A

    A - AtCoder Group Contest Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statem ...

  7. 并查集 HDOJ 1232 畅通工程

    题目传送门 /* 并查集(Union-Find)裸题 并查集三个函数:初始化Init,寻找根节点Find,连通Union 考察:连通边数问题 */ #include <cstdio> #i ...

  8. magento package

    Magento Local module file is relative to app/code/local/ Magento Community module file is relative t ...

  9. UESTC - 878 温泉旅店 二维费用背包问题

    http://acm.uestc.edu.cn/#/problem/show/878 设dp[i][j][k]表示在前i个数中,第一个得到的异或值是j,第二个人得到的异或值是k的方案数有多少种. 因为 ...

  10. java学习第二章