HDU 6178 Monkeys
题意:给出一棵 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的更多相关文章
- 2017多校第10场 HDU 6178 Monkeys 贪心,或者DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:给出一棵有n个节点的树,现在需要你把k只猴子放在节点上,每个节点最多放一只猴子,且要求每只 ...
- HDU 6178 Monkeys(树上的二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:现在有一n个顶点的树形图,还有k只猴子,每个顶点只能容纳一只猴子,而且每只猴子至少和另外一只猴子通过 ...
- 【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys
http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每 ...
- HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)
There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...
- 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 ...
- hdu 3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- hdu 4414 Finding crosses【简单模拟】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)
「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...
- ActiveMQ的单节点和集群部署
平安寿险消息队列用的是ActiveMQ. 单节点部署: 下载解压后,直接cd到bin目录,用activemq start命令就可启动activemq服务端了. ActiveMQ默认采用61616端口提 ...
- TabLayout实现底部导航栏(2)
TabLayout是android.support.design里的一个控件,使用它可以很方便的做出顶部导航和底部导航.类似于这样的,能设置选中时字体的颜色和选中时的图片. 效果如图: 首先我们在 b ...
- poi生成excel报表合并列
功能任务 poi导出excel统计报表,有合并列的. 根据结构生成层级. 目标 1输入一个linkshashmap LinkedHashMap<String, Object> fieldM ...
- 8086实时时钟实验(一)——《x86汇编语言:从实模式到保护模式》05
1.代码清单 ;代码清单9-1 ;文件名:c09_1.asm ;文件说明:用户程序 ;创建日期:2011-4-16 22:03 ;=================================== ...
- js之cookie操作
1.使用document.cookie保存信息 document.cookie = "Test=cooo";alert(document.cookie); //hours为空字符串 ...
- 通过js控制层的动态隐藏
<style type="text/css"> #dv1{ width:1000px; height:1000px; overflow:hidden; display: ...
- redis的有序集合(Sorted Sets)数据类型
和Sets相比,Sorted Sets增加了一个权重参数score,使得集合中的元素能够按score进行有序排列,比如一个存储全班同学成绩的Sorted Sets,其集合value可以是同学的学号,而 ...
- Java内部类详解 2
Java内部类详解 说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就 ...
- Django组件——分页器(paginator)
一.视图层 from django.shortcuts import render # Create your views here. from .models import Book from dj ...