题意:给出一棵 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. java中的线程(3):线程池类 ThreadPoolExecutor「线程池的类型、参数、扩展等」

    官方文档: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html 1.简介 pu ...

  2. Ubuntu安装Docker步骤

    环境:Ubuntu Trusty 14.04 (LTS) 前提条件: Docker requires a 64-bit installation regardless of your Ubuntu v ...

  3. Python 中的反射和自省

    本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...

  4. elasticsearch fitler查询例子

  5. 使用awstat分析Nginx的访问日志

    在我的上一篇文章<使用 Nginx 提升网站访问速度>中介绍了 Nginx 这个 HTTP 服务器以及如何通过它来加速网站的访问速度.在实际的网站运营中,我们经常需要了解到网站的访问情况, ...

  6. JVM 类加载全过程

    类加载机制 - JVM把class文件加载到内存中 并对数据进行 校验,解析,初始化,最终形成JVM可以直接使用的java类型的过程 详细过程  加载→ 验证→ 准备→ 解析 → 初始化→ 使用 → ...

  7. pat04-树4. Root of AVL Tree (25)

    04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  8. POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 86780   ...

  9. nginx反向代理使用网址速度变慢

    最近公司网址加载静态文件的速度总是跟不上于是试着用带端口的ip来访问, 发现速度快不少于是将nginx的代理修改为ip的如: location / { proxy_pass http://localh ...

  10. bzoj 5340: [Ctsc2018]假面

    Description 题面 Solution 生命值范围比较小,首先维护每一个人在每个血量的概率,从而算出生存的概率,设为 \(a[i]\) 询问时,只需要考虑生存的人数,可以 \(DP\) 设 \ ...