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 ...
随机推荐
- log4j常用的配置文件
# priority :debug<info<warn<error #you cannot specify every priority with different file fo ...
- STS插件创建springboot项目,pom第一行报unkown错误
Description Resource Path Location TypeUnknown pom.xml /amq-provider line 1 ...
- struts2的相关知识(实现原理、拦截器)
struts2的实现原理 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的 ...
- 1.Linux安装redis
Linux安装redis 操作系统是Centos7 1.下载压缩包 2.解压 3.编译 4.启动redis 5.设置redis.conf和防火墙端口开放,外网可以访问 1.下载压缩包 下载地址:htt ...
- [luogu] P3809 【模板】后缀排序 (SA)
板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...
- php接受post传值的方法
这段时间在研究php的接口,利用jmeter模拟发送数据给php服务器,看php如何接收post传输过去的数据,遇到了几个问题,经过一番度娘之后终于有所理解,记录一下: 这里记录常用的两种post方式 ...
- layer.js插件
官方网址: http://layer.layui.com/
- SPFA的两个优化
评测题:洛谷[模板]单源最短路径 不加任何优化: queue<int>q; void spfa(ll s) { ;i<=n;i++) d[i]=(ll)(); d[s]=;q.pus ...
- sts创建maven项目 引入spring,报错
症状: Missing artifact org.springframework:spring-core:jar:5.0.0.RC3 原因: 在引入之前没有设置spring版本号 和spring ur ...
- 再探容斥好题——ROOK
这个时候考过:安师大附中集训 Day2 当时看shadowice1984的做法,但是没有亲自写,,, 雅礼集训考试的时候鼓捣半天,被卡常到80pts,要跑9s 卡不动. 正解实际是: 3重容斥 1.随 ...