CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接>
题目大意:
给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出。
解题分析:
比较经典的一种树形DP的模型。
$dp1[u]$表示以$u$为根的子树中最少需要翻转的边数(即$u$走到子树中所有的点需要翻转的边数),$dp2[u]$表示u向父亲方向走,需要翻转的边数。
$dp1$的转移方程很好写:$dp1[u]=dp1[u]+e[i].w$ (正向边$e[i].w=0$,反向边为1)
$dp2$的转移方程通过图像也能够比较直观的得到:$dp2[v]=dp1[u]-dp1[v]-e[i].w+e[i\^{1}].w+dp2[u];$
#include <bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T&x){
x=;int f=;char c=getchar();
while(c<''||c>''){ if(c=='-')f=-;c=getchar(); }
while(c>='' && c<=''){ x=x*+c-''; c=getchar(); }
x*=f;
}
const int N = 2e5+;
#define REP(i,s,t) for(int i=s;i<=t;i++)
struct Edge{ int to,w,nxt; }e[N<<];
int n,cnt;
int head[N],dp1[N],dp2[N];
inline void add(int u,int v,int w){
e[cnt]=(Edge){v,w,head[u] };head[u]=cnt++;
}
void dfs1(int u,int pre){
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(v==pre)continue;
dfs1(v,u);
dp1[u]+=dp1[v]+e[i].w;
}
}
void dfs2(int u,int pre){
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(v==pre)continue;
dp2[v]=dp1[u]-dp1[v]+dp2[u]+(e[i].w?-:);
//dp2[v]=dp1[u]-dp1[v]-e[i].w+e[i^1].w+dp2[u];
//dp1[u]-dp1[u]-e[i].w+e[i^1].w表示v的父亲u的子树中,除v的子树的其它部分需要翻转的边数(从v向上走时),dp2[u]表示u向上的方向需要翻转的变数
dfs2(v,u);
}
}
int main(){
read(n);
memset(head,-,sizeof(head));
REP(i,,n-){
int u,v;read(u);read(v);
add(u,v,);add(v,u,);
}
dfs1(,-);dfs2(,-);
int ans=1e9;
REP(i,,n)ans=min(ans,dp1[i]+dp2[i]);
cout<<ans<<endl;
REP(i,,n)if(ans==dp1[i]+dp2[i])cout<<i<<' ';
}
CodeForces 219D Choosing Capital for Treeland (树形DP)经典的更多相关文章
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- 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)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
随机推荐
- Nginx优化_自定义报错页面
自定义返回给客户端的404错误页面 1. 优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到 client]# firefox http://192.168.4.5/xxxxx ...
- javaweb各种框架组合案例(九):springboot+tk.mybatis+通用service
一.项目结构 二.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 我是如何用python给Thunar写GUI插件的 (pygtk+glade)
更新:zip乱码的问题可以通过安装patch之后的p7zip-natspec和unzip-natspec解决(archlinuxcn源),而仍使用Engrampa做前端.此文重点在pygtk... 问 ...
- SSM架构 (Spring 5.0.2)添加Jackson
第一步添加jsckson的包 <dependency> <groupId>javax.annotation</groupId> <artifactId> ...
- [apache] apache配置文件中的deny和allow
allow 和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 例如: <Di ...
- JS中判断一个数组是否有相同数据的
页面中有多个<select> $("select").each(function(){ str.push($(this).val());}); // join() 方法 ...
- Ubuntu 18.04 安装 Octave 5.1
最新版目前只能通过编译安装.折腾了半天终于搞定: 需要使用apt-get install先把各种 dependencies 安装好. 编译JIT需要安装sudo apt-get install llv ...
- luoguP2365 任务安排 斜率优化 + 动态规划
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- REST服务地址
https://www.cnblogs.com/loveis715/p/4669091.html https://www.cnblogs.com/duanxz/p/4384454.html REST服 ...
- vue键盘修饰符
keyup事件 <input type='input' @keyup="keyEvent"> keyup.enter事件 <input type='input' ...