Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D
题意
给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个答案按顺序全部输出。
分析
把边的方向化为权值,正向为0,逆向为1。问题转化为找哪些点的在遍历全图后总权值最小。这就是树形DP了,它可以从子树收获价值,也可以从父亲收获。所以dfs两遍。
定义dp[u][0]为以u为根的的子树全可达的修改次数,随便选定一个点,用子节点信息更新父节点,dp[u][0]+=dp[v][0]+w;
再定义dp[u][1]为以u为根的整颗树需要修改的次数,根据第一次dp的结果,利用父节点的信息来更新子节点。
如果fa指向u,那么dp[u][1]=dp[fa][1]-1;如果u指向fa,那么dp[u][1]=dp[fa][1]+1;
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+;
//const int MAXN = 210;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case #%d: ",++T);
}
const int MAXN = 2e5+;
const int MAXM = ; struct node{
int to,w,nxt;
}edge[MAXN<<]; int cnt,head[MAXN];
int dp[MAXN][]; void init(){
cnt=;
mset(head,-);
} void addEdge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].nxt=head[u];
head[u]=cnt++;
} void sear(int u,int pa){
dp[u][]=;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].to;
if(v!=pa){
sear(v,u);
dp[u][]+=(dp[v][]+edge[i].w);
}
}
}
void dfs(int u,int pa,int x){
if(u==pa){
dp[u][]=dp[u][];
}else{
if(x==) dp[u][]=dp[pa][]+;//pa->u
else dp[u][]=dp[pa][]-;//u->pa
}
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].to;
if(v!=pa){
dfs(v,u,edge[i].w);
}
}
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int n;
scd(n);
int u,v;
init();
for(int i=;i<n;i++){
scdd(u,v);
addEdge(u,v,);
addEdge(v,u,);
}
sear(,-);
// for(int i=1;i<=n;i++) printf("%d ",dp[i][0]);puts("");
dfs(,,);
int ans=inf;
for(int i=;i<=n;i++) ans=min(ans,dp[i][]);
cout<<ans<<endl;
for(int i=;i<=n;i++){
if(ans==dp[i][]){
printf("%d ",i);
}
}
return ;
}
Codeforces 219D - Choosing Capital for Treeland(树形dp)的更多相关文章
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- CF 219D Choosing Capital for Treeland 树形DP 好题
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
随机推荐
- Export OracleDB Schema To Doc
叶正盛先生做的mini工具. https://github.com/rgqancy/DBExportDoc 使用中遇到过两个小问题: 1.数据库的表名不能带.(我一个很初级的同事建立的数据库是demo ...
- js中如何优雅的写if判断
我们编写js代码时经常遇到复杂逻辑判断的情况,通常大家可以用if/else或者switch来实现多个条件判断,但这样会有个问题,随着逻辑复杂度的增加,代码中的if/else/switch会变得越来越臃 ...
- 使用ssh config配置文件来管理ssh连接
我本人其实及其烦使用配置文件这种东西,有时候看到巨大又复杂的配置文件,甚至复杂过代码的时候,总感觉设计配置文件的人有些本末倒置. 但是ssh这个配置文件真的非常简单好用,让我稍微体验了一次配置文件使用 ...
- free命令详解
free的命令详解 free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 语法 free [选项] 选项 -b 以Byte为单位显示内存的使用情况 -k 以K ...
- prettier & codes format
prettier & codes format https://prettier.io/playground/ https://github.com/collections/front-end ...
- c-lodop云打印实现手机打印 JS语句打印
Lodop和c-lodop目前只能安装到windows操作系统上,但是其他操作系统可通过向C-Lodop安装的电脑发送打印任务,实现手机广域网或局域网打印,打印语句也是简单的JS语句,可以轻松实现云打 ...
- Jquery_如何扩展方法
jQuery 别名 $ 一. 类级别扩展方法(比如$.ajax(...)) 1> 单个全局方法 $.testExtend = function (){ console.log("单个 ...
- 我的G++编译选项
-Wall -Wextra -g3 -DLOCAL -Wshadow -Wpointer-arith -Wcast-qual -Waggregate-return -Winline -Wunreach ...
- BZOJ 3612: [Heoi2014]平衡
3612: [Heoi2014]平衡 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 283 Solved: 219[Submit][Status][ ...
- 找到第一个只出现一次的字符并返回它的位置(Python)
s = 'hellobaby' def findchar(s): for i in s: if s.count(i)==1: return i, s.index(i) m,n=findchar(s) ...