Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D
题意:
给你一棵树,n个节点。
树上的边都是有向边,并且不一定是从父亲指向儿子的。
你可以任意翻转一些边的方向。
现在让你找一个节点,使得从这个节点出发能够到达其他所有节点,并保证翻转边的数量最小。
问你最少翻转多少条边,并输出所有满足此条件的节点编号。
题解:
本题要解两个dp: dp1 & dp2
首先考虑dp1:
表示状态:
dp1[i]表示使节点i能够到达i的子树中的所有节点,翻转边的最小数量。
如何转移:
dp1[i] = ∑ (dp1[son] + 边<i,son>由i指向son ? 0 : 1)
dfs一遍即可。
然后求dp2:
表示状态:
dp2[i]表示使节点i能够到达这棵树的所有节点,翻转边的最小数量。
如何转移:
dp2[i] = dp2[par] + 边<par,i>是否由par指向i ? 1 : -1
如果边<par,i>由par指向i,那么在dp2[par]中这条边是不会被翻转的,所以此时应该将它翻转,代价+1。
如果边<par,i>由i指向par,那么在dp2[par]中这条边已经被翻转了一次,然而在dp2[i]中是不需要翻转的,所以将dp2[par]中多余的那次翻转减掉就好。
边界条件:
dp2[1] = dp1[1]
(默认根节点为1)
所以最终答案为dp2[i]中的最小值,然后将所有dp2[i]等于ans的节点输出就好啦。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX_N 200005
#define INF 1000000000 using namespace std; struct Edge
{
int dest;
bool flag;
Edge(int _dest,bool _flag)
{
dest=_dest;
flag=_flag;
}
Edge(){}
}; int n;
int ans=INF;
int dp1[MAX_N];
int dp2[MAX_N];
vector<Edge> edge[MAX_N]; void read()
{
cin>>n;
int x,y;
for(int i=;i<n;i++)
{
cin>>x>>y;
edge[x].push_back(Edge(y,true));
edge[y].push_back(Edge(x,false));
}
} void dfs1(int now,int p)
{
dp1[now]=;
for(int i=;i<edge[now].size();i++)
{
Edge temp=edge[now][i];
if(temp.dest!=p)
{
dfs1(temp.dest,now);
dp1[now]+=dp1[temp.dest]+(!temp.flag);
}
}
} void dfs2(int now,int p,bool d)
{
if(now==) dp2[now]=dp1[now];
else dp2[now]=dp2[p]+(d?:-);
ans=min(ans,dp2[now]);
for(int i=;i<edge[now].size();i++)
{
Edge temp=edge[now][i];
if(temp.dest!=p) dfs2(temp.dest,now,temp.flag);
}
} void work()
{
dfs1(,-);
dfs2(,-,);
cout<<ans<<endl;
for(int i=;i<=n;i++)
{
if(dp2[i]==ans) cout<<i<<" ";
}
cout<<endl;
} int main()
{
read();
work();
}
Codeforces 219D Choosing Capital for Treeland:Tree dp的更多相关文章
- 【题解】codeforces 219D Choosing Capital for Treeland 树型dp
题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...
- Codeforces 219D Choosing Capital for Treeland(树形DP)
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)
题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...
- Codeforces 219D Choosing Capital for Treeland 2次DP
//选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
随机推荐
- centos7 配置ssh 免密码登陆
我只有一台机器,是因为要配置hadoop分布式环境用,需要配置ssh 两个用户: zhangxs, root 首先在切换到zhangxs用户下 执行[ ssh-keygen -t rsa] [zhan ...
- DirectShow使用心得
用了3天时间,将DShow加入到了游戏中,记录下心得,方便要使用的童鞋以及以后的自己查看.1. Video Mixing Renderer 9,内部使用Direct3D 9,需要Windows XP或 ...
- ubunut jdk 配置
sudo mv jdk1.7.0_10 /usr/lib/jvm/ sudo gedit /etc/profile 在profile中加入下面内容: export JAVA_HOME=/usr/lib ...
- Power of Cryptography - poj 2109
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20351 Accepted: 10284 Description C ...
- 使用Eclipse自带的Maven插件创建Web项目时报错:
问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-a ...
- Linux64位程序移植
1 概述 Linux下的程序大多充当服务器的角色,在这种情况下,随着负载量和功能的增加,服务器所使用内存必然也随之增加,然而32位系统固有的4GB虚拟地址空间限制,在如今已是非常突出的问题了:另一个需 ...
- 九度OJ 1339:ACM (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:712 解决:379 题目描述: 今年的ACM世界总决赛快要开始了,需要有一个排名算法来对每支队伍进行现场排名.ACM组委会把这个任务交给了你 ...
- anaconda + opencv3
直接运行 pip install opencv-python 或者 pip install opencv-contrib-python 参照如下网页 https://blog.csdn.net/sin ...
- vMware存储:SAN配置基础
VMware存储不仅仅是将LUN映射给物理服务器这么简单.VMware vSphere允许系统管理员在一个物理机上创建多个虚拟机. 潜在的hypervisor和vSphere ESXi,能够使gues ...
- python selenium cookie 登录
概要: 1.正常登录,使用selenium获取cookie: 2.保存cookie: 3.使用cookie登录. 4.python--2.7,selenium--3.4.1 步骤1 正常登录,使用se ...