[codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边。
解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值。依然是两次dfs,套路解法。
对树形dp的理解:树形dp其实就是将树进行暴力搜索,只是需要理解状态的概念。那些状态已经完成,需要从底还是从顶开始搜索。
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<cstdlib>
- #include<cmath>
- #include<iostream>
- using namespace std;
- typedef long long ll;
- const int maxn=1e6+;
- const int inf=0x3f3f3f3f;
- int head[maxn],tot,n,m,sum;
- int dp[maxn][],cnt[maxn];
- struct edge{
- int to;
- int nxt;
- int w;
- }e[maxn<<];
- void add_edge(int u,int v,int w){
- e[tot].to=v;
- e[tot].w=w;
- e[tot].nxt=head[u];
- head[u]=tot++;
- }
- void dfs1(int u,int fa){
- for(int i=head[u];i!=-;i=e[i].nxt){
- int v=e[i].to;
- if(v==fa) continue;
- dfs1(v,u);
- dp[u][]+=dp[v][]+e[i].w;
- }
- }
- void dfs2(int u,int fa){
- for(int i=head[u];i!=-;i=e[i].nxt){
- int v=e[i].to;
- if(v==fa) continue;
- dp[v][]=dp[u][]-dp[v][]+dp[u][];
- if(e[i].w==) dp[v][]+=;
- else dp[v][]-=;
- dfs2(v,u);
- }
- }
- inline int read(){
- char k=;char ls;ls=getchar();for(;ls<''||ls>'';k=ls,ls=getchar());
- int x=;for(;ls>=''&&ls<='';ls=getchar())x=(x<<)+(x<<)+ls-'';
- if(k=='-')x=-x;return x;
- }
- int main(){
- int k=;
- while(scanf("%d",&n)!=EOF){
- memset(head,-,sizeof head);
- tot=;
- int a,b;
- /*for(int i=1;i<n;i++){
- cnt[i]=read();
- sum+=1ll*cnt[i];
- }*/
- for(int i=;i<n-;i++){
- a=read();
- b=read();
- add_edge(a,b,);
- add_edge(b,a,);
- }
- dfs1(,-);
- dfs2(,-);
- for(int i=;i<=n;i++){
- dp[i][]=dp[i][]+dp[i][];
- }
- int min1=inf;
- for(int i=;i<=n;i++){
- min1=min(dp[i][],min1);
- }
- cout<<min1<<"\n";
- for(int i=;i<=n;i++){
- // cout<<dp[i][2]<<" ";
- if(dp[i][]==min1) cout<<i<<" ";
- }
- }
- return ;
- return ;
- }
[codeforces219D]Choosing Capital for Treeland树形dp的更多相关文章
- 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 ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
随机推荐
- /etc/init.d/iptables stop
/etc/init.d/iptables stop systemctl stop firewalld.service [root@bigdata-server-01 myrestserve ...
- Wireshark学习笔记——怎样高速抓取HTTP数据包
0.前言 在火狐浏览器和谷歌浏览器中能够很方便的调试network(抓取HTTP数据包),可是在360系列浏览器(兼容模式或IE标准模式)中抓取HTTP数据包就不那么那么方便了.尽管也可使用H ...
- Pentaho BIServer Community Edtion 6.1 使用教程 第一篇 软件安装
一.简介: Pentaho BI Server 分为企业版和社区版两个版本.其中 社区版 CE(community edtion) 为免费版本. 二.下载CE版(CentOS): 后台下载命令: no ...
- CSS3 3D下拉折叠菜单
在线演示 本地下载
- 冷门PHP函数汇总
概述 整理一些日常生活中基本用不到的PHP函数,也可以说在框架内基本都内置了,无需我们去自行使用的函数.量不多.后续在日常开发中如遇到更多的冷门,会更新本文章 sys_getloadavg 获取系统的 ...
- CentOS7 网络管理工具nmcli
今天帮别人调试虚拟机的网络问题(CentOS 7系统),习惯性直接改/etc/sysconfig/network-scripts/ifcfg-xxx配置文件,但是不知道为什么重启network后静态i ...
- 发现eclipse红叉,查看markers发现Target runtime Apache Tomcat v8.0 is not defined
导入以前的项目(Markers中注意查看,就在console选项卡旁边),报以下错误,但不影响操作: Faceted Project Problem Target runtime Apa ...
- Java_泛型_01_T与?
二.参考文档 1.JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别
- Listen81
Nut-Cracking Chimps Demonstrate Cultural Differences One family generally dines on Chinese takeout w ...
- animation-delay负值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...