CF 219D

【题目链接】CF 219D

【题目类型】树形DP

&题意:

给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到u去,则要逆转该边方向)如果有多个这样的点,则升序输出所有。

&题解:

参考自:http://blog.csdn.net/angon823/article/details/52316220

需要求每个点到其他n-1个点是否要逆转路径,那么就可以把正的路cost=1,逆转的cost=0,这只要求这个点到其他点的cost,之后用所有的也就是n-1条边减去这个就好了。求一个点到其他点的cost,可以用树形DP来求,u点的cost=u子树的cost+其他的cost

&代码:

  1. #include <map>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <iostream>
  6. #include <set>
  7. #include <queue>
  8. #include <cstring>
  9. #include <algorithm>
  10. using namespace std;
  11. #define cle(a,v) memset(a,(v),sizeof(a))
  12. #define ll long long
  13. const int maxn = 2e5 + 7;
  14. struct Edge {
  15. int v, w, next;
  16. } edges[maxn * 2];
  17. int head[maxn], tot;
  18. void addedge(int u, int v, int w) {
  19. edges[tot] = Edge{v, w, head[u]};
  20. head[u] = tot++;
  21. }
  22. int n, dpson[maxn], dpfa[maxn];
  23. void dfs1(int u, int fa) {
  24. for (int i = head[u]; ~i; i = edges[i].next) {
  25. int v = edges[i].v;
  26. int w = edges[i].w;
  27. if (v != fa) {
  28. dfs1(v, u);
  29. dpson[u] += dpson[v] + w;
  30. }
  31. }
  32. }
  33. void dfs2(int u, int fa) {
  34. for (int i = head[u]; i != -1; i = edges[i].next) {
  35. int v = edges[i].v;
  36. int w = edges[i].w;
  37. if (v == fa) continue;
  38. dpfa[v] = dpfa[u] + (w ? 0 : 1) + dpson[u] - dpson[v] - w;
  39. dfs2(v, u);
  40. }
  41. }
  42. int main() {
  43. freopen("1.in", "r", stdin);
  44. cle(head, -1); tot = 0;
  45. cle(dpson, 0);
  46. cle(dpfa, 0);
  47. scanf("%d", &n);
  48. for (int i = 1; i < n; i++) {
  49. int u, v;
  50. scanf("%d%d", &u, &v);
  51. addedge(u, v, 1);
  52. addedge(v, u, 0);
  53. }
  54. dfs1(1, -1);
  55. dfs2(1, -1);
  56. vector<pair<int, int>> vp;
  57. for (int i = 1; i <= n; i++) {
  58. vp.push_back(make_pair(dpson[i] + dpfa[i], i));
  59. }
  60. sort(vp.begin(), vp.end());
  61. int t1 = vp[n - 1].first;
  62. int i;
  63. for (i = n - 2; i >= 0; i--) {
  64. if (vp[i].first != t1) {
  65. break;
  66. }
  67. }
  68. printf("%d\n", n - 1 - vp[n - 1].first);
  69. for (int j = i + 1; j < n; j++) {
  70. printf("%d%c", vp[j].second, j == n - 1 ? '\n' : ' ');
  71. }
  72. return 0;
  73. }

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

  1. CodeForces 219D 树形DP

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

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

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

  3. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

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

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

  5. CF 486D vailid set 树形DP

    As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...

  6. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  7. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  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. Codeforces 219D - Choosing Capital for Treeland(树形dp)

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

随机推荐

  1. tensorflow结果可视化-【老鱼学tensorflow】

    这次我们把上次的结果进行可视化显示,我们会把神经网络的优化过程以图像的方式展示出来,方便我们了解神经网络是如何进行优化的. 首先,我们把测试数据显示出来: # 显示测试数据 fig = plt.fig ...

  2. .Net Core WebAPI 搭建

    .Net Core WebAPI 搭建 1.创建项目 使用开发工具为 Visual Studio 2017 2.创建 Controller 实体类 public class Book { public ...

  3. iOS12系统应用发送普通邮实现发送

    iOS12系统应用发送普通邮实现发送 构建好邮件以后,可以发送该邮件.此时需要使用mailComposeDelegate属性,该属性用来设置委托,其语法形式如下: unowned(unsafe) va ...

  4. Fragment概述

    1 Fragment Fragment是什么? Fragment允许将Activity拆分成多个完全独立封装的可重用的组件,每个组件有它自己的生命周期和UI布局. 每个Fragment都是独立的模块, ...

  5. Python开发实战PDF

    Python开发实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1iP9VmwuzDMfdZTfpupR3CA 提取码:a523 复制这段内容后打开百度网盘手机A ...

  6. 作用域和闭包(二)this

    this 要在执行时才确认,定义时无法确认 1. 作为构造函数执行 2. 作为对象属性执行 3.作为普通函数执行 4. call,apply,bind 改变this

  7. float和double的最大值和最小值

  8. 推送测试,生产环境无法打印log获取deviceToken,可以通过弹窗获取deviceToken

    z- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:( ...

  9. sql的基本语句

    SQL中的inner join, left join, right join, full join 创建两个测试表并且输入相关值create table test_a(aid int,aNum var ...

  10. Autofac之实例获取

    通过之前的学习了解了使用Autofac第一步是通过向容器中注册类型,然后从容器中获取类型实例该怎么做呢? 本次主要学习如何在Autofac中获取注册类型实例 Resolve 使用Resolve方法获取 ...