<题目链接>

题目大意:

给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出。

解题分析:

比较经典的一种树形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)经典的更多相关文章

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

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

  2. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  3. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

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

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

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

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

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

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

  7. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  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. chattr 改变文件的扩展属性

    1. 命令功能 chattr和lsattr用来改变文件.目录属性和查看这种文件属性:chmod只是改变文件的读.写.执行权限,更底层的属性控制是由chattr来改变. 2. 语法格式 chattr [ ...

  2. VPS建站

    参考腾讯云的教程 选择了 LAMP的方案,即Linux + Apache + MySQL + Php 参考链接 https://cloud.tencent.com/edu/learning/cours ...

  3. CSS3选择器 ::before和::after

    :before和:after伪元素的用法 :before和:after伪元素的用法十分简单:下面的代码样例中, :before 和 :after 将会在 blockquote 元素之前和之后插入新内容 ...

  4. web--响应式导航菜单

    响应式导航菜单 代码如下 HTML代码: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  5. -bash: findstr: command not found 问题解决

    今天有个任务,需要获取apk的版本信息,百度之后说是之下下面的命令就行 adb shell dumpsys package com.baidu.searchbox | findstr versionC ...

  6. 026:if标签使用详解

    if标签使用详解: if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号  {%  %}  进行包裹. if 标签中可以使 ...

  7. swagger2接口发布demo

    1.目的:使用Swagger2发布接口,ui可操作 2.项目结构  3. 代码 3.1 接口类qinfeng.zheng.api.controller.DemoController package q ...

  8. (线性基)Operation

    http://acm.hdu.edu.cn/showproblem.php?pid=6579 线性基https://blog.csdn.net/a_forever_dream/article/deta ...

  9. 170817关于AJAX的知识点

    1.AJAX                  [1] AJAX简介                        全称: Asynchronous JavaScript And XML        ...

  10. noi.ac #712 练级

    分析 把船当作点 练级当作边 发现一个连通块大于n-1的边的条数的奇偶性影响这个连通块的答案 于是并查集维护即可 代码 #include<bits/stdc++.h> using name ...