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

这题挺好想的,因为做过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. XSS跨站脚本攻击实例讲解,新浪微博XSS漏洞过程分析

    2011年6月28日晚,新浪微博遭遇到XSS蠕虫攻击侵袭,在不到一个小时的时间,超过3万微博用户受到该XSS蠕虫的攻击.此事件给严重依赖社交网络的网友们敲响了警钟.在此之前,国内多家著名的SNS网站和 ...

  2. Hadoop 免密码登陆(ssh)

    record save here first [root@hadoop .ssh]# ssh-keygen -t rsa -P ''Generating public/private rsa key ...

  3. IOS 中的CoreImage框架

    IOS 中的CoreImage框架(framework) - time4cnblogs 时间 2014-03-15 00:24:00  博客园-所有随笔区原文  http://www.cnblogs. ...

  4. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  5. Android创建桌面快捷图标

    <!-- 添加快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL ...

  6. linux ls正则表达式

    ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...

  7. shell脚本步骤调试

    第一种方式===> [root@localhost functions]# sh -x test.sh --check xx+ '[' --check '!=' -check ']'+ case ...

  8. 【OpenStack】OpenStack系列6之Sheepdog环境搭建

    准备 repo配置 yum clean all yum makecache yum install -y make automake autoconf gcc nss-devel wget git g ...

  9. IOC原理解释

    spring ioc它其实是一种降低对象耦合关系的设计思想,通常来说,我们在一个类调用另一个类的方法的时候,需要不断的new新的对象来调用该方法,类与类之间耦合度比较高,有了ioc容器以后,ico容器 ...

  10. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...