题目是给一张边有向的树形图。要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向。问可以选哪几个点作为首都,使它们所需反转边的数量最少。

这题挺好想的,因为做过HDU2196

  • 首先就不妨设正向边权值为0,反向边权值为1,那样就是各个点出发到其他点经过边所需的最少权值和。
  • 然后对于每个点,分两个部分考虑:以这个点为根的子树这个点往上走的部分
    1. dp[0][u]表示以u点作为首都且以u点为根的子树部分所需反转边的数量,容易知道就等于子树内边权和
    2. dp[1][u]表示以u点作为首都且u点向上部分所需反转边的数量,画下图就知道怎么转移了:dp[1][v] = ( dp[0][u]-dp[0][v]-weight(u,v) ) + dp[1][u] + weight(v,u) (v是u的孩子)
  • 这样最后对于每个点u,它的答案就是这两部分之和了,即dp[0][u]+dp[1][u]。

感觉又学到一种树形DP的新姿势:分别考虑点往下的子树和点往上的父亲部分。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 222222
struct Edge{
int u,v,w,next;
}edge[MAXN<<];
int NE,head[MAXN];
void addEdge(int u,int v,int w){
edge[NE].u=u; edge[NE].v=v; edge[NE].w=w;
edge[NE].next=head[u]; head[u]=NE++;
}
int d[][MAXN];
void dfs0(int u,int fa){
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
dfs0(v,u);
d[][u]+=d[][v]+edge[i].w;
}
}
void dfs1(int u,int fa){
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
d[][v]=d[][u]-d[][v]-edge[i].w+d[][u]+edge[i^].w;
dfs1(v,u);
}
}
int main(){
memset(head,-,sizeof(head));
int n,a,b;
scanf("%d",&n);
for(int i=; i<n; ++i){
scanf("%d%d",&a,&b);
addEdge(a,b,); addEdge(b,a,);
}
dfs0(,);
dfs1(,);
int res=INF;
for(int i=; i<=n; ++i) res=min(res,d[][i]+d[][i]);
printf("%d\n",res);
for(int i=; i<=n; ++i){
if(res==d[][i]+d[][i]){
printf("%d ",i);
}
}
return ;
}

Codeforces 219D Choosing Capital for Treeland(树形DP)的更多相关文章

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

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

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

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

  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/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

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

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

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

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

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

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

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

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

  9. [codeforces219D]Choosing Capital for Treeland树形dp

    题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...

随机推荐

  1. 不同版本的name可以重复

    - validates :name, presence: true, uniqueness: { conditions: -> { where(:state.ne => 2) } }, l ...

  2. 压缩 javascript 和 css

    www.iwangzheng.com 目前我们项目中的 CSS/JS 文件比较多, 由于RAILS 3.0 没有提供asset pipeline功能,所以这样会制约我们的访问速度. 例如:  目前,我 ...

  3. [BZOJ2303][Apio2011]方格染色

    [BZOJ2303][Apio2011]方格染色 试题描述 Sam和他的妹妹Sara有一个包含n × m个方格的 表格.她们想要将其的每个方格都染成红色或蓝色. 出于个人喜好,他们想要表格中每个2 × ...

  4. (转)WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  5. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  6. JAVA接口继承、抽象类等

    1.定义接口 package test.intefaces; public interface TestIntefaceA { void testA(); void testB(); void tes ...

  7. Android 中的openurl

    在iOS中这么叫,在android中不知道是不是这么称呼. 转发一篇文章 http://www.cnblogs.com/zhangkai5157/p/3289532.html

  8. codeforces A. Dima and Continuous Line 解题报告

    题目链接:http://codeforces.com/problemset/problem/358/A 题目意思:在横坐标上给出n个不同的点,需要把前一个点跟后一个点(这两个点的顺序是紧挨着的)用一个 ...

  9. FZU 2140 Forever 0.5 (几何构造)

    Forever 0.5 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  10. 开启InnoDB每表一个独立的表空间

    mysql> show variables like '%innodb%'; +---------------------------------+----------------------- ...