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 ...
随机推荐
- 数据插入INSERT
一.INSERT SELECT :将查询的数据直接插入 特点: 1.一次性插入所有查询出来的数据. 2.数据原子性,有一个失败全部失败. 3.没有指定的列加默认值或NULL,都没有就报错. 二.INS ...
- win10 装centos7 虚拟机
1.下载VMware Workstation 64版本 https://www.vmware.com/products/workstation-pro/workstation-pro-evaluati ...
- 红米note_维修_开机键
1. 2.在线人工客服(20180919) 很荣幸为您服务,有什么问题可以帮助到您的- 我的手机 后边的 开机键 貌似 不太行了 您好,您是哪款手机 就是 要按 好几次 很用力 才能 开亮手机屏幕木 ...
- Quartz.NET 作业调度使用
Quartz.NET的使用方法有很多,今天使用Quartz.NET3.0.6的时候发现和2.0版本的语法不太一样,百度上找了一圈也没有找到解决办法 后来在GitHub上下载源代码解决了 实现每隔10s ...
- vue之mapMutaions的使用 && vuex中 action 用法示例 && api.js的使用
vue之mapMutations的使用 我们通过Mutation来改变store中的state,方法往往是在子组件中使用 this.$store.commit(); 来实现,但是这样的缺点是不容易查看 ...
- 案例41-hibernate练习-添加客户
1 utils部分 1 HibernateUtils package www.test.utils; import org.hibernate.Session; import org.hibernat ...
- vim源码编译启用python
坑:只指定with-python-config-dir没有指定enable-pythoninterp是没有用的 ./configure --enable-pythoninterp --with-pyt ...
- FZU 2207 ——以撒的结合——————【LCA + 记录祖先】
Problem 2207 以撒的结合 Accept: 47 Submit: 161Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- MySQL -U防止人为误操作
在很多时候操作数据库的时候,可能领导或DBA登陆了数据库,在执行update和delete时,忘记了加where,可能会导致清空表的悲剧,所以-U的好处就体现了. 1.mysql -U的帮助说明 -U ...
- SSO单点登录三种情况的实现方式详解(转)
https://blog.csdn.net/ainuser/article/details/65631713