树形dp(C - Choosing Capital for Treeland CodeForces - 219D )
题目链接:https://cn.vjudge.net/contest/277955#problem/C
题目大意:输入n,代表有n个城市,然后再输入n-1条有向边,然后让你找出一个改变边数的最小值,使得某个城市能够到达剩余的所有城市,然后问这样的城市有多少个,并且输出这些城市的编号。
具体思路:我们首先按照题目给的条件建好边,然后树就建好了,对于当前的某个节点,如果这个点要是能够到达剩余的所有城市,从这个节点往上的话,这个节点连接的父亲节点的这条边应该是是从子节点到父节点的。从这个节点往下的话,这个节点往下的话,他所连接的子节点应该是往下的,具体思路就来了。
用dp[i][0]代表当前的节点往下连接他的子树所需的最小的改变的边的数目。dp[i][1]代表的是当前的子节点往上连接他的父亲节点所需的改变最小的边数。
第二种情况,dp[rt][0]+=dp[soon][0];
第一种情况,dp[soon][1]+=dp[rt][1]+dp[rt][0]-edge[i].cost-dp[to][0]+(edge[i].cost==1?0:1).
AC代码:
#include<iostream>
#include<cmath>
#include<stack>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 4e5+;
struct node
{
int nex;
int to;
int cost;
} edge[maxn];
int num,head[maxn],dp[maxn][],father[maxn];
int sto[maxn];
void init()
{
num=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int fr,int to,int cost)
{
edge[num].to=to;
edge[num].nex=head[fr];
edge[num].cost=cost;
head[fr]=num++;
}
void dfs1(int fr,int rt)
{
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dfs1(to,fr);
dp[fr][]+=dp[to][]+edge[i].cost;
}
}
void dfs2(int fr,int rt)
{
for(int i=head[fr]; i!=-; i=edge[i].nex)
{
int to=edge[i].to;
if(to==rt)
continue;
dp[to][]+=dp[fr][]+dp[fr][]-dp[to][]+(edge[i].cost==?:)-edge[i].cost;
dfs2(to,fr);
}
}
int main()
{
init();
int n;
scanf("%d",&n);
int t1,t2;
for(int i=; i<=n-; i++)
{
scanf("%d %d",&t1,&t2);
addedge(t1,t2,);
addedge(t2,t1,);
}
dfs1(,-);
dfs2(,-);
int minn=inf;
for(int i=; i<=n; i++)
{
minn=min(minn,dp[i][]+dp[i][]);
}
printf("%d\n",minn);
int num=;
for(int i=; i<=n; i++)
{
if(dp[i][]+dp[i][]==minn)
{
sto[++num]=i;
}
}
sort(sto+,sto+num+);
for(int i=; i<=num; i++)
{
if(i==)
printf("%d",sto[i]);
else
printf(" %d",sto[i]);
}
printf("\n");
return ;
}
树形dp(C - Choosing Capital for Treeland CodeForces - 219D )的更多相关文章
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- 树形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 ...
- (纪念第一道完全自己想的树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 ...
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs
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)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
随机推荐
- Mysql 数据锁与事务
一.锁 常用命令 查看表的存储引擎:mysql> show create table myLock; 修改当前表的存储引擎:mysql> alter table myLock engine ...
- linux系统日志__ratelimit: N callbacks suppressed
报错 今天线上遇到故障,php进行因为段错误退出了,系统日志中的kernel报错如下: Feb 25 22:25:11 web_server_01 kernel: __ratelimit: 250 c ...
- CSU1911 Card Game 【FWT】
题目链接 CSU1911 题解 FWT模板题 #include<algorithm> #include<iostream> #include<cstdlib> #i ...
- 使用LD_Preload的Linux权限升级技巧
0x00 前言 共享库是程序在启动时加载的库.正确安装共享库后,之后启动的所有程序将自动使用新的共享库. 0x01 共享库名称 每个共享库都有一个名为soname的特殊名称.soname有前缀li ...
- 使用cmstp绕过应用程序白名单
默认情况下,AppLocker允许在文件夹中执行二进制文件,这是可以绕过它的主要原因.已经发现,这样的二进制文件可以很容易地用于绕过AppLocker和UAC.与Microsoft相关的二进制文件之一 ...
- 开源入侵检测系统SELKS系统搭建
一.系统环境配置 系统环境:centos7x64 ip地址:172.16.91.130 1.设置静态IP地址 [root@localhost backlion]#vi /etc/sys ...
- Mac显示同一程序的所有窗口
通过F3键可以显示所有程序的所有窗口到桌面,但是如果窗口太多的话显示出来很难看清. 如果只显示某一个程序的所有窗口就不会乱了. 其实在dock上有一个相应的选项,但是没有快捷键. 开启方法: 在终端上 ...
- Java EE之通过表单上传文件
public class Ticket { private String customerName; private String subject; private String body; priv ...
- js中相等、大小 不同类型之间是如何进行对比的。
上个小问题 [] > [] false [] < [] false [] == [] false // why? 再上个加强版 '6xxx' < '5xx' false '6xxx' ...
- App统计指标定义
度量(指标) 定义 活跃用户 指启动应用的用户(去重,即1台设备打开多次会被计为1个活跃用户). 是衡量一个应用运营情况最基础的一个指标,用以表示用户规模.通常根据不同的时间限定,有日活跃用户.周活跃 ...