Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图。要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向。问可以选哪几个点作为首都,使它们所需反转边的数量最少。
这题挺好想的,因为做过HDU2196。
- 首先就不妨设正向边权值为0,反向边权值为1,那样就是各个点出发到其他点经过边所需的最少权值和。
- 然后对于每个点,分两个部分考虑:以这个点为根的子树、这个点往上走的部分:
- dp[0][u]表示以u点作为首都且以u点为根的子树部分所需反转边的数量,容易知道就等于子树内边权和
- 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)的更多相关文章
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
随机推荐
- HDU 3998 Sequence(经典问题,最长上升子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3998 解题报告:求一个数列的最长上升子序列,并求像这样长的不相交的子序列最多有多少个. 我用的是最简单 ...
- Coursera台大机器学习课程笔记10 -- Linear Models for Classification
这一节讲线性模型,先将几种线性模型进行了对比,通过转换误差函数来将linear regression 和logistic regression 用于分类. 比较重要的是这种图,它解释了为何可以用Lin ...
- FastMM的安装方法
FastMM 快速在D2006和2007中已代替了原来的内存管理器.D7也可以使用,而且很方便哦.请看步骤: 1. FastMM是开源项目,去她老家先拖个来. http://sourceforge.n ...
- Linux 日志文件utmp、wtmp、lastlog、messages
1.有关当前登录用户的信息记录在文件utmp中:==who命令 2.登录进入和退出纪录在文件wtmp中:==w命令 3.最后一次登录文件可以用lastlog命令察看: 4.messag ...
- 如何在Linux上实现文件系统的自动检查和修复?
Linux文件系统有可能在各种各样的情况下受到损坏,比如系统崩溃.突然断电.磁盘断开,或者文件节点 (i-node)不小心被覆盖等等,因此需要定期检查文件系统,而说到检查和修复Linux文件系统,fs ...
- 【云计算】开源装机自动化系统 CloudBoot OSInstall 介绍
"CloudBoot"(OSinstall) 发布了. 产品更新及特点如下: 新增虚拟化操作系统适配:支持主流操作系统:RedHat.CentOS.SUSE.Ubuntu.Wind ...
- A + B Problem
Write a function that add two numbers A and B. You should not use + or any arithmetic operators. 分析: ...
- placement new讲解
[本文链接] http://www.cnblogs.com/hellogiser/p/placement-new.html [分析] 首先我们区分下几个容易混淆的关键词:new.operator ne ...
- Android涉及到的设计模式
转载地址:http://blog.csdn.net/dengshengjin2234/article/details/8502097 1.适配器模式:ListView或GridView的Adapter ...
- 重命名nginx服务器
为了防止被黑客扫描到web服务器信息,通过相对应的web服务器信息找出对应的版本漏洞,从而对web服务器进行入侵,nginx虽然功能强大,但是也是软件,软件就可能会有漏洞,例如nginx-0.6.32 ...