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 ...
随机推荐
- Delphi处理Http请求自定义Header
在HTTP请求中,get方法是默认的,但在URL地址长度是有限的,请求方法能传送的数据也是有限的,一般get方法传递的数据不能大于2KB,当get请求方法传递的数据长度不能满足需求时,就需要采用另一种 ...
- php实现常驻进程 多进程监控
php都是通过crontabd定时脚本处理队列的,面试被问到php如何常驻进程进行处理队列,想了半天这样不知道是否是一种方式 <?php function logs(){ file_put_co ...
- schema举例二
schema举例二: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs=& ...
- selenium之调用Javascript
selenium调用Javascript使用方法: driver.execute_script(js) 使用JS获取元素文本值,代码片段如下: ...... js = "return $(' ...
- BZOJ3510 首都(LCT)
即动态维护树的重心.考虑合并后的新重心一定在两棵树的重心的连线上.于是对每个点维护其子树大小,合并时在这条链的splay上二分即可.至于如何维护子树大小,见https://blog.csdn.net/ ...
- ROADS POJ - 1724(分层最短路)
就是在最短路的基础上 多加了一个时间的限制 , 多一个限制多一维就好了 记住 分层最短路要用dijistra !!! #include <iostream> #include < ...
- apache 限制连接并发数和下载速度
apache 限制并发数 需要安装:mod_limitipconn模块 安装方法:yum install mod_limitipconn 安装完成后: 检查 Apache 模块目录,看是否含有 mod ...
- 洛谷P1973 [NOI2011]Noi嘉年华(动态规划,决策单调性)
洛谷题目传送门 DP题怕是都要大大的脑洞...... 首先,时间那么大没用,直接离散化. 第一问还好.根据题意容易发现,当一堆活动的时间有大量重叠的时候,更好的办法是把它们全部安排到一边去.那么我们转 ...
- 洛谷P4768 [NOI2018]归程(可持久化并查集,最短路)
闲话 一个蒟蒻,在网络同步赛上进行了这样的表演-- T2组合计数不会,T3字符串数据结构不会,于是爆肝T1 一开始以为整个地图都有车,然后写了2h+的树套树,终于发现样例过不去 然后写可持久化并查集D ...
- 【bzoj3876】 Ahoi2014—支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 (题目链接) 题意 给出一张拓扑图,每条边有一个权值,问每次从1号点出发,走遍所有的边所需要的 ...