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 ...
随机推荐
- python格式化字符串Type Error: Format Requires Mapping 的问题
最近几天 频繁看到有这种写法 BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s" 第一次看到的pythoner看到可能会有点懵逼 ...
- Check failed: status == CUBLAS_STATUS_SUCCESS (11 vs. 0) CUBLAS_STATUS_MAPPING_ERROR
I0930 21:23:15.115576 30918 solver.cpp:281] Learning Rate Policy: multistepF0930 21:23:17.263314 310 ...
- BZOJ1861[Zjoi2006]书架——非旋转treap
题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些 ...
- BZOJ5306 HAOI2018染色(容斥原理+NTT)
容易想到枚举恰好出现S次的颜色有几种.如果固定至少有i种恰好出现S次,那么方案数是C(M,i)·C(N,i*S)·(M-i)N-i*S·(i*S)!/(S!)i,设为f(i). 于是考虑容斥,可得恰好 ...
- First Knight UVALive - 4297(优化高斯消元解概率dp)
题意: 一个矩形区域被分成 m*n 个单元编号为 (1, 1)至 (m, n),左上为 (1, 1),右下为(m, n).给出P(k)i,j,其中 1 ≤ i ≤ m,1 ≤ j ≤ n,1 ≤ k ...
- 自定义缓存设计(static缓存数据)
设计题 编程过程中,为了解决缓存数据共享的问题,常常会使用static关键字达到脱离具体实例化对象,在整个java进程生命周期内共享数据的目的.请编写一个类,类名为MapCache,拥有但不局限于以下 ...
- MT【61】含参数二次函数最大最小值
评:此类题目在高考中作为压轴题也曾考过,一般通性通法都如上面的做法,但是我们如果可以站在包络的角度,很多问题将变得很清晰:
- MT【12】三点坐标求面积
$L_1,L_2$是O发出的两条射线,C是一个常数,一条动直线$l$分别与$L_1,L_2$交于A,B两点.$S_{\Delta ABC}=C$,求A,B的中点D的轨迹方程.(2012北大自主招生) ...
- WebSocket安卓客户端实现详解(三)–服务端主动通知
WebSocket安卓客户端实现详解(三)–服务端主动通知 本篇依旧是接着上一篇继续扩展,还没看过之前博客的小伙伴,这里附上前几篇地址 WebSocket安卓客户端实现详解(一)–连接建立与重连 We ...
- 【BZOJ4161】Shlw loves matrixI (常系数齐次线性递推)
[BZOJ4161]Shlw loves matrixI (常系数齐次线性递推) 题面 BZOJ 题解 \(k\)很小,可以直接暴力多项式乘法和取模. 然后就是常系数齐次线性递推那套理论了,戳这里 # ...