[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 ...
随机推荐
- python 基础 1.2--pycharm 的安装及使用
一. windows 先安装pycharm. PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,其提供了一个带编码补全,代码片段,支持代码折 ...
- Java 学习 day06
01-面向对象(Static关键字) package myFirstCode; /* 静态:static. 用法:是一个修饰符,用于修饰成员(成员变量,成员函数) 当成员被静态修饰后,就多了一个调用方 ...
- 【BZOJ4399】魔法少女LJJ 线段树合并
[BZOJ4399]魔法少女LJJ Description 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的 ...
- MANIFEST.MF 文件内容完全详解(转)
打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介绍MANI ...
- java常用的基础容器
1 Vector and ArrayList 它们都是可以随机访问的.它们的区别是Vector是线程安全的,而ArrayList不是线程安全的. 2 HashMap的底层实现机制 2.1 底层数据结构 ...
- 【题解】[CJOI2019Chebnear]
[题解][CJOI2019Chebnear] 题目描述 给定平面上有\(n\)个仇人,\((x,y) ,x,y \in R^+\) ,\(n\)个人都是仇人关系,而有仇人关系的一对人的切比雪夫距离若\ ...
- 【足迹C++primer】35、特定容器算法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/33732681 特定容器算法 lst.me ...
- DLL中导出ANSI和UNICODE函数
模仿window中的DLL导出ANSI和UNICODE版本的函数,使用UNICODE宏来控制使用哪个版本: 在函数实际的执行代码UNICODE版本中,在ANSI函数的版本中只做参数的转换,及ANSI字 ...
- video codec 学习笔记
一. H.264 (http://www.baike.com/wiki/H264) 三大标准: AVC(Advanced Video Coding,AVC) H.264,同时也是MPEG-4第十部分 ...
- ActiveMQ之发布、订阅使用
maven依赖 <dependencies> <dependency> <groupId>org.apache.activemq</groupId> & ...