CF#135 D. Choosing Capital for Treeland 树形DP
D. Choosing Capital for Treeland
题意
给出一颗有方向的n个节点的树,现在要选择一个点作为首都。
问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都可以是哪些点。
思路
先忽略掉树中的方向,dp[i]表示i节点到它的子树所有点最少需要翻转的边。
进行第一遍dfs
如果u-v的方向是u-->v,那么dp[u]=dp[u]+dp[v];,否则dp[u]=dp[u]+dp[v]+1;,表示u-v这条边要翻转。
这时根节点的dp值就是根节点作为首都需要翻转的边,进行第二遍dfs:
dp[i]表示i作为首都需要翻转的最少边的数量
如果u-v的方向是u-->v,那么dp[v]=dp[u]+1;,否则dp[v]=dp[u]-1。
代码
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
vector<int>vec[N],ans;
int n,dp[N];
map<int,map<int,int> >mp;
void dfs(int u,int fa)
{
for(int v:vec[u])
{
if(v==fa) continue;
dfs(v,u);
dp[u]+=(dp[v]+!mp[u][v]);
}
}
void dfs2(int u,int fa)
{
for(int v:vec[u])
{
if(v==fa) continue;
if(mp[u][v]) dp[v]=dp[u]+1;
else dp[v]=dp[u]-1;
dfs2(v,u);
}
}
int main()
{
scanf("%d",&n);
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=1;//mp[u][v]==1,表示u-v的方向是u-->v
vec[u].pb(v);
vec[v].pb(u);
}
dfs(1,0);
dfs2(1,0);
int maxn=inf;
for(int i=1; i<=n; i++)
{
if(dp[i]<maxn)
{
maxn=dp[i];
ans.clear();
ans.pb(i);
}
else if(dp[i]==maxn)
ans.pb(i);
}
printf("%d\n",maxn);
for(int v:ans)
printf("%d ",v);
printf("\n");
return 0;
}
CF#135 D. Choosing Capital for Treeland 树形DP的更多相关文章
- 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 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- [codeforces219D]Choosing Capital for Treeland树形dp
题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...
- CodeForces 219D Choosing Capital for Treeland (树形DP)经典
<题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...
- 【CF】135 Div2 Choosing Capital for Treeland
树形结构,挺有意思的题目.不难. /* 219D */ #include <iostream> #include <string> #include <map> # ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- CF 219 D:Choosing Capital for Treeland(树形dp)
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
随机推荐
- E - Roaming Atcoder
题解:https://blog.csdn.net/qq_40655981/article/details/104459253 题目大意:n个房间,,每个房间都有一个人,一共k天,在一天,一个人可以到任 ...
- V - Infinite Prefixes CodeForces - 1295B math
天哪!!菜到家啦. 数学+思维. 首先求出一个周期内cnt0-cnt1=c的个数,如果C=0,那么只要在一个周期内有前缀等于x,那么答案就是-1,否则答案就是0 如果C!=0,列一下方程x=t*c+a ...
- react: typescript import images alias
1.webpack.config.js resolve: { extensions: ["ts", "tsx", "js", "j ...
- Java 网络编程 -- 基于TCP 模拟多用户登录
Java TCP的基本操作参考前一篇:Java 网络编程 – 基于TCP实现文件上传 实现多用户操作之前先实现以下单用户操作,假设目前有一个用户: 账号:zs 密码:123 服务端: public c ...
- thinkphp if便签的使用
<foreach name="list" item='v'> <tr> <td><img class="user" s ...
- thinkphp--多表查询
我们可以将两个表连起来一起查询数据,我现在有两张表,一个是feedback表和member表,如图: 总目录: 上代码: $where = array(); $"; $Model = M(' ...
- 使用sys模块写一个软件安装进度条
import sys,time for i in range(50): sys.stdout.write('#') sys.stdout.flush() #强制刷新将内存中的文件写一条,输出一条. t ...
- ReentrantReadWriteLock及共享锁的实现
介绍 ReentrantReadWriteLock是j.u.c包下提供的ReadWriteLock接口的实现. ReadWriteLock作为读写锁,提供了返回读锁和返回写锁两个方法. /** * 读 ...
- MongoDB学习(四):通过Java使用MongoDB
环境配置 在Java项目中使用MongoDB,需要在项目中引入mongo.jar这个包.下载地址:下载 请尽量下载较新的版本,本文用的是2.10.1. 连接MongoDB public synchro ...
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...