<题目链接>

题目大意:

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

解题分析:

比较经典的一种树形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. 详解webpack4打包--新手入门(填坑)

    注意,这个dev和build好像在哪儿见过??对了, 刚刚才在package.json里配置的“scripts”这一项的值就有“dev”和“build”.对,一点都不错,就是这2个值,这2个值代表的是 ...

  2. MongoDb学习 自定义配置mongodb连接

    最近研究了mongodb获取本地连接属性的方案,场景就是mongodb的连接地址不在配置文件中配置,而是在代码中写好,代码中是从本地文件读取地址. public class MongoConfig { ...

  3. [php代码审计] Window api函数 FindFirstFile 在php中的问题

    include文件夹中文件: 内容: <?php echo __FILE__; ?> index.php: 演示如下: “<<”替换多个任意字符: “>”替换单个字符:

  4. springCloud 服务提供者应返回的统一的数据格式

    package com.zledu.commonentity.entity; import lombok.AllArgsConstructor;import lombok.Data; import j ...

  5. UML建模重点圈划

    面向对象的特征 *P9*>封装性>继承性>多态性>传递性 建模语言的三个类别 *P14*> - 非形式化的.半形式化的和形式化的 UML 特点*15*主要有三个特点:&g ...

  6. 洛谷 P1407 稳定婚姻

    问题描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关.25岁的姗姗和男友谈恋爱半年就结婚,结婚不到两 ...

  7. 未能加载文件或程序集 ICSharpCode.SharpZipLib

    Ui调用Webservice ,Webservice调用Bl BL中明明有那个dll文件 后来发现是webservice里面没有 在附加调试的时候,断点在bl层中

  8. python 全栈开发,Day9(函数的初始,返回值,传参,三元运算)

    一.函数的初始 比如python没有len()方法,如果求字符串的长度 使用for循环 s = 'asdfadsf' count = 0 for i in s: count += 1 print(co ...

  9. HTTP请求时候总是设置的两个参数ConnectionTimeOut和SocketTimeOut

    在HTTP请求时候总是设置两个参数,就是连接超时和Socket超时 public static final String SO_TIMEOUT = "http.socket.timeout& ...

  10. MyView.java 自己画的view

    package myapplication21.lum.com.mycanvas; import android.content.Context;import android.graphics.Can ...