题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边。

解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值。依然是两次dfs,套路解法。

对树形dp的理解:树形dp其实就是将树进行暴力搜索,只是需要理解状态的概念。那些状态已经完成,需要从底还是从顶开始搜索。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<iostream>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn=1e6+;
  10. const int inf=0x3f3f3f3f;
  11. int head[maxn],tot,n,m,sum;
  12. int dp[maxn][],cnt[maxn];
  13. struct edge{
  14. int to;
  15. int nxt;
  16. int w;
  17. }e[maxn<<];
  18. void add_edge(int u,int v,int w){
  19. e[tot].to=v;
  20. e[tot].w=w;
  21. e[tot].nxt=head[u];
  22. head[u]=tot++;
  23. }
  24.  
  25. void dfs1(int u,int fa){
  26. for(int i=head[u];i!=-;i=e[i].nxt){
  27. int v=e[i].to;
  28. if(v==fa) continue;
  29. dfs1(v,u);
  30. dp[u][]+=dp[v][]+e[i].w;
  31. }
  32. }
  33.  
  34. void dfs2(int u,int fa){
  35. for(int i=head[u];i!=-;i=e[i].nxt){
  36. int v=e[i].to;
  37. if(v==fa) continue;
  38. dp[v][]=dp[u][]-dp[v][]+dp[u][];
  39. if(e[i].w==) dp[v][]+=;
  40. else dp[v][]-=;
  41. dfs2(v,u);
  42. }
  43. }
  44.  
  45. inline int read(){
  46. char k=;char ls;ls=getchar();for(;ls<''||ls>'';k=ls,ls=getchar());
  47. int x=;for(;ls>=''&&ls<='';ls=getchar())x=(x<<)+(x<<)+ls-'';
  48. if(k=='-')x=-x;return x;
  49. }
  50.  
  51. int main(){
  52. int k=;
  53. while(scanf("%d",&n)!=EOF){
  54. memset(head,-,sizeof head);
  55. tot=;
  56. int a,b;
  57. /*for(int i=1;i<n;i++){
  58. cnt[i]=read();
  59. sum+=1ll*cnt[i];
  60. }*/
  61. for(int i=;i<n-;i++){
  62. a=read();
  63. b=read();
  64. add_edge(a,b,);
  65. add_edge(b,a,);
  66. }
  67. dfs1(,-);
  68. dfs2(,-);
  69. for(int i=;i<=n;i++){
  70. dp[i][]=dp[i][]+dp[i][];
  71. }
  72. int min1=inf;
  73. for(int i=;i<=n;i++){
  74. min1=min(dp[i][],min1);
  75. }
  76. cout<<min1<<"\n";
  77. for(int i=;i<=n;i++){
  78. // cout<<dp[i][2]<<" ";
  79. if(dp[i][]==min1) cout<<i<<" ";
  80. }
  81. }
  82. return ;
  83. return ;
  84. }

[codeforces219D]Choosing Capital for Treeland树形dp的更多相关文章

  1. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  2. CF219D. Choosing Capital for Treeland [树形DP]

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

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

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

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

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

  5. CodeForces 219D Choosing Capital for Treeland (树形DP)经典

    <题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...

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

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

  7. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  8. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  9. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)

    Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...

随机推荐

  1. /etc/init.d/iptables stop

    /etc/init.d/iptables stop     systemctl stop firewalld.service   [root@bigdata-server-01 myrestserve ...

  2. Wireshark学习笔记——怎样高速抓取HTTP数据包

    0.前言     在火狐浏览器和谷歌浏览器中能够很方便的调试network(抓取HTTP数据包),可是在360系列浏览器(兼容模式或IE标准模式)中抓取HTTP数据包就不那么那么方便了.尽管也可使用H ...

  3. Pentaho BIServer Community Edtion 6.1 使用教程 第一篇 软件安装

    一.简介: Pentaho BI Server 分为企业版和社区版两个版本.其中 社区版 CE(community edtion) 为免费版本. 二.下载CE版(CentOS): 后台下载命令: no ...

  4. CSS3 3D下拉折叠菜单

    在线演示 本地下载

  5. 冷门PHP函数汇总

    概述 整理一些日常生活中基本用不到的PHP函数,也可以说在框架内基本都内置了,无需我们去自行使用的函数.量不多.后续在日常开发中如遇到更多的冷门,会更新本文章 sys_getloadavg 获取系统的 ...

  6. CentOS7 网络管理工具nmcli

    今天帮别人调试虚拟机的网络问题(CentOS 7系统),习惯性直接改/etc/sysconfig/network-scripts/ifcfg-xxx配置文件,但是不知道为什么重启network后静态i ...

  7. 发现eclipse红叉,查看markers发现Target runtime Apache Tomcat v8.0 is not defined

    导入以前的项目(Markers中注意查看,就在console选项卡旁边),报以下错误,但不影响操作: Faceted Project Problem        Target runtime Apa ...

  8. Java_泛型_01_T与?

    二.参考文档 1.JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别

  9. Listen81

    Nut-Cracking Chimps Demonstrate Cultural Differences One family generally dines on Chinese takeout w ...

  10. animation-delay负值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...