题意:给出一棵 N 个节点树,上面有 K 个猴子,然后竟可能删边,但是每一只猴子必须有直接相邻的猴子与之相邻。求最少剩下几条边。

分析:一条边可以用两只猴子站,这样的一条点对,越多越好,如果是ans个,ans*2>=k,那么只需要 (k+1)/2 条边。

否则,需要  ans + (k-ans*2) 条边。

现在问题就转为求这样的点对有多少,哈哈,有漏洞的DP都AC了,哈哈~~~~。

我发现标程思路也是这样,哈哈,都是bug程序!!!

唯一的解释就是 d[u][1] >= d[u][0] 感性认识

#include <bits/stdc++.h>

using namespace std;

const int maxn = ;

vector<int> g[maxn];

/*------- 开挂 -------*/
namespace fastIO {
#define BUF_SIZE 100000
// fread -> read
bool IOerror = ; char nc() {
static char buf[BUF_SIZE], *pl = buf + BUF_SIZE, *pr = buf + BUF_SIZE;
if(pl == pr) {
pl = buf;
pr = buf + fread(buf, , BUF_SIZE, stdin);
if(pr == pl) {
IOerror = ;
return -;
}
}
return *pl++;
} inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
} void read(int &x) {
char ch;
while(blank(ch = nc()));
if(IOerror)
return;
for(x = ch - ''; (ch = nc()) >= '' && ch <= ''; x = x * + ch - '');
}
#undef BUF_SIZE
};
using namespace fastIO;
/*------- 完结 -------*/ int d[maxn][]; // 0 匹配
bool vis[maxn]; void dp(int u,int fa) {
if(vis[u]) return;
vis[u] = ; d[u][] = d[u][] = ;
int sum = ;
for(int i=; i < (int)g[u].size(); i++) {
int v = g[u][i];
if(v==fa) continue;
dp(v,u);
d[u][] += max(d[v][],d[v][]);
sum+=d[v][];
} for(int i=; i < (int)g[u].size(); i++) {
int v = g[u][i];
if(v==fa) continue;
d[u][] = max(d[u][],sum-d[v][]+d[v][]+);
} } int T;
int N,K;
int main()
{
//freopen("in.txt","r",stdin);
read(T);
while(T--) {
read(N);
read(K); for(int i=; i <= N; i++)
g[i].clear();
memset(vis,,sizeof(vis)); int u;
for(int i=; i < N; i++) {
read(u);
g[u].push_back(i+);
g[i+].push_back(u);
} dp(,-); int ans = max(d[][],d[][]);
if(ans*>=K)
printf("%d\n",(K+)/);
else
printf("%d\n",ans+K-(ans*));
} return ;
}

HDU 6178 Monkeys的更多相关文章

  1. 2017多校第10场 HDU 6178 Monkeys 贪心,或者DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:给出一棵有n个节点的树,现在需要你把k只猴子放在节点上,每个节点最多放一只猴子,且要求每只 ...

  2. HDU 6178 Monkeys(树上的二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:现在有一n个顶点的树形图,还有k只猴子,每个顶点只能容纳一只猴子,而且每只猴子至少和另外一只猴子通过 ...

  3. 【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys

    http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每 ...

  4. HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)

    There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...

  5. hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1

    J - Infinite monkey theorem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  6. hdu 3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  8. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  9. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. mysql 数据存储引擎区别

    一,存储类型 二 , MyISAM默认存储引擎 MyISAM 管理非事务表.是ISAM 的扩展格式.除了提供ISAM里所没有的索引的字段管理等的大量功能.MyISAM 还使用一种表格锁定的机制.来优化 ...

  2. Go初探

      官方网站:https://golang.org/ 标准库文档:https://golang.org/pkg/ 在线编码学习:https://play.golang.org/ PS:请自行FQ 简介 ...

  3. Proguard breaking audio file in assets or raw

    http://stackoverflow.com/questions/21440572/proguard-breaking-audio-file-in-assets-or-raw Issue: I h ...

  4. Java面试题搜集

    这里是一些Java面试题,从"程序员小灰"公众号转载过来,备用. 项目介绍 明确项目是做什么的 明确项目的价值.(为什么做这个项目,它解决了用户什么痛点,它带来什么价值?) 明确项 ...

  5. 0.数据结构(python语言) 基本概念 算法的代价及度量!!!

    先看思维导图: *思维导图有点简陋,本着循循渐进的思想,这小节的知识大多只做了解即可. *重点在于算法的代价及度量!!!查找资料务必弄清楚. 零.四个基本概念 问题:一个具体的需求 问题实例:针对问题 ...

  6. 【linux相识相知】sed命令

    在之前的博客中我们介绍了文本三剑客中grep,本次博客就另外一名剑客——sed做出详细的描述,sed真的是一款强大的工具.下面让我们来一起看一下吧! 概述和工作机制 SED的英文全称为Stream E ...

  7. iis6、iis7、apache设置mime类型

    1.IIS6添加方法. 打开iis,展开网站,右键要设置的站点--属性.找到“http头”选项卡--mime类型 进行设置添加. 截图以.ipa mime类型举例. 2.IIS7(iis7.5.iis ...

  8. PHP常用的一些数组操作总结

    1.array_values() :返回包含数组中所有键值的数组,不保留键名. 2.array_diff() 函数返回两个数组的差集数组.该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的 ...

  9. 【Java集合】LinkedList详解前篇

    [Java集合]LinkedList详解前篇 一.背景 最近在看一本<Redis深度历险>的书籍,书中第二节讲了Redis的5种数据结构,其中看到redis的list结构时,作者提到red ...

  10. oracle之数据同步:Oracle Sql Loader使用说明(大批量快速插入数据库记录)

    1.准备表数据 select * from emp10; create sequence seq_eseq increment start maxvalue ; --得到序列的SQL语句 select ...