[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 ...
随机推荐
- Android 六大存储
Android平台进行存储的方式: 一.使用SharedPreferences存储 二.文件存储数据 三.SQLite数据库存储 四.使用ContentProvider存储数据 五.网络存储数据 今天 ...
- 远程访问(post 传参数) 以及IOUtils复制文件
package com.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream ...
- 九度OJ 1167:数组排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5395 解决:1715 题目描述: 输入一个数组的值,求出各个值从小到大排序后的次序. 输入: 输入有多组数据. 每组输入的第一个数为数组的 ...
- Pentaho BIServer Community Edtion 6.1 使用教程 第二篇 迁移元数据 [HSQLDB TO MySQL]
第一部分 迁移原因 Pentaho BI 社区版服务的很多元数据信息存储在数据库汇总,其默认使用HSQLDB 数据库,即借助它存储自身的资料库,比如 Quartz 调度信息.业务资料库连接信息(数据 ...
- PHP生成图片太慢了。。有些都不出来、
现在为了使用不同宽高的图片,做了个动态生成的程序.每次根据图片传入的宽高来输出图片,然后 html 页面里用 <img src="xxx.com/img?src=c8d997dae15 ...
- windows下python安装face_recognition模块
安装face_recognition出现报错,要先安装dlib 安装dlib时报错,要先安装cmake cmake安装成功后,用pip安装dlib失败 从pypi下载dlib的wheel文件,然后用w ...
- vs2012环境配置
快捷键 css格式设置 字体设置 新建项目 项目创建失败? 更改默认开发语言环境 1.快捷键 代码格式化:Ctrl+K+D 2.css格式设置: 工具→选项→文本编辑器→CSS→格式设置→选择“紧凑模 ...
- Delphi编写WebService体会
源:Delphi编写WebService体会 Dispatch: 派遣,分派 Invoke: 调用 Invokable: 可调用接口 TReomtable: WebService中自定义类都是继承自该 ...
- python中的编码转换
今天遇到了一个问题,将字符串“\uxxxx\uxxxx”转换成汉字.网上查了很多资料都不行. 后来看到,发现一个函数就OK了. str = str.decode('unicode_escape') 等 ...
- python学习笔记:第五天( 列表、元组)
Python3 列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...