传送门啦

这个树形dp就没那么简单了,运用了一下贪心的思想

不同的排序方法对应着不同的转移方程,如果我们用 $ f[x] = max(f[x] , b[i] +cnt - i + 1) $ 来进行转移就要从小往大排,才能使f[x]小,如果用 $ f[x] = max(f[x] , b[i] + i - 1) $ 来转移就要从大往小排序。

第一个转移方程:$ cnt- i $ 为还有多少个才能到它,然后+1是因为国王这个点信息需要1的时间。即他子树的大小+传递到他的时间+1(向下传递)

第二个转移方程类似。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005; inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
} int n,p;
int head[maxn],tot;
int f[maxn],minn=1e9;
int ans[maxn]; struct Edge{
int to,next;
}edge[maxn << 1]; void add(int u,int v){
edge[++tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
} void dfs(int x,int fa){
int b[550],cnt = 0;
for(int i=head[x];i;i=edge[i].next){
int v = edge[i].to;
if(v != fa){
dfs(v , x);
b[++cnt] = f[v];
}
}
sort(b + 1, b + 1 + cnt );
for(int i=1;i<=cnt;i++)
f[x] = max(f[x] , b[i] + cnt - i + 1);
} int main(){
n = read();
for(int i=2;i<=n;i++){
p = read();
add(i , p);
add(p , i);
}
for(int i=1;i<=n;i++){
memset(f , 0 , sizeof(f));
dfs(i , 0);
minn = min(minn , f[i]);
ans[i] = f[i];
}
printf("%d\n",minn+1);
for(int i=1;i<=n;i++)
if(ans[i] == minn) printf("%d ",i);
return 0;
}

第二个:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005; inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
} int n,p;
int head[maxn],tot;
int f[maxn],minn=1e9;
int ans[maxn]; struct Edge{
int to,next;
}edge[maxn << 1]; void add(int u,int v){
edge[++tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
} bool cmp(int x , int y){
return x > y;
} void dfs(int x,int fa){
int b[550] = {0},cnt = 0;
for(int i=head[x];i;i=edge[i].next){
int v = edge[i].to;
if(v != fa){
dfs(v , x);
b[++cnt] = f[v];
}
}
sort(b + 1, b + 1 + cnt , cmp);
for(int i=1;i<=cnt;i++)
f[x] = max(f[x] , b[i] + i - 1);
f[x] += 1;
} int main(){
n = read();
for(int i=2;i<=n;i++){
p = read();
add(i , p); add(p , i);
}
for(int i=1;i<=n;i++){
memset(f , 0 , sizeof(f));
dfs(i , 0);
minn = min(minn , f[i]);
ans[i] = f[i];
}
printf("%d\n",minn);
for(int i=1;i<=n;i++)
if(ans[i] == minn) printf("%d ",i);
return 0;
}

洛谷P2018消息传递的更多相关文章

  1. 洛谷P2018 消息传递

    P2018 消息传递 题目描述 巴蜀国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级.如果A是B的上级,B是C的上级,那么A就是C的上级.绝对不会出现这样的关系:A是B的 ...

  2. 洛谷——P2018 消息传递

    P2018 消息传递 题目描述 巴蜀国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级.如果A是B的上级,B是C的上级,那么A就是C的上级.绝对不会出现这样的关系:A是B的 ...

  3. 洛谷 P2018 消息传递 题解

    题面 总体来说是一道从下往上的DP+贪心: 设f[i]表示将消息传给i,i的子树全部接收到所能消耗的最小时间: 那么对于i的所有亲儿子节点j,我们会贪心地先给f[j]大的人传递,然后次大..... 可 ...

  4. 洛谷 P2018 消息传递

    题目分析 贪心+树形DP 本来还以为要大费周折地换根,然后发现 \(n\) 很小,可以直接 \(O(n^2\log n)\) 枚举. 枚举每个节点作为根,用 \(f_x\) 表示走完以 \(x\) 为 ...

  5. 洛谷NOIp热身赛题解

    洛谷NOIp热身赛题解 A 最大差值 简单树状数组,维护区间和.区间平方和,方差按照给的公式算就行了 #include<bits/stdc++.h> #define il inline # ...

  6. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  7. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  8. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  9. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

随机推荐

  1. CentOS 6.6下配置本地yum源与网络yum源

    一.本地yum源 1.系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@localhost ~]# cd /etc/yum.repos.d/                    ...

  2. 300. Longest Increasing Subsequence_算法有误

    300. Longest Increasing Subsequence 300. Longest Increasing Subsequence Given an unsorted array of i ...

  3. Docker入门与应用系列(三)容器管理

    一.启动容器 启动容器有两种方式,一种是基于镜像新建一个容器并启动,另一个是将终止状态的容器重新启动. 1.1 新建并启动 主要命令为 docker run 下面的命令输出一个”Hello,world ...

  4. NATS_07:NATS之top工具监控以及测量调优工具

    概述 你可以使用 nats-top 来实现类似于 linux 中 top 命令的实时监控 nats 服务: 可以使用 nats 提供的工具来进行针对性的调优. 安装nats-top $ go get ...

  5. pyqt5的代码

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  6. JS动态更新微信浏览器中的title

    问题: 最近在做一个微信中分享的宣传页,分不同的场景,切换不同的场景时需要设置不同的title,实现的方案很简单,当用户切换场景的时候,修改document对象的title属性,可是在实际测试中,io ...

  7. IntelliJ IDEA中Java类注释

    打开Idea,依次选择File-->Settings-->Editor-->File and CodeTemplates ,右侧tab选项卡点击Includes,选择File Hea ...

  8. angularJS $routeProvider

    O'Reilly书上的伪代码 var someModule = angular.module('someModule',[...module dependencies]); someModule.co ...

  9. 6个动作4种难度选择!家庭减肥就用hiit

    今天推荐一组课程计划,6个动作,后面会教你如何调整课程难度,以便让课程更适合自己的身体情况. 一.深蹲:8-10次 二.俯卧撑:5-8次(女生如果完成不了标准俯卧撑,可以选择跪姿俯卧撑) 三.平板支撑 ...

  10. asp.net菜鸟到中级程序员的飞跃 --30本好书点评

    如果你是一个菜鸟或者自认为初学者那么本文非常适合你:不能说这30本书就是最佳组合,但是可以说这个组合不差:本人曾博览群书,很多书重复,很多书讲的不适用,这些书都是目前书店可以买到的:达到中级程序员以后 ...