http://acm.hdu.edu.cn/showproblem.php?pid=6178

题意:
现在有一n个顶点的树形图,还有k只猴子,每个顶点只能容纳一只猴子,而且每只猴子至少和另外一只猴子通过边相连,现在要删边,保留最少的边使得满足题意。

思路:

贪心的想一想,顶点两两匹配时一条边的贡献值就是两只猴子,那这不就是二分匹配吗。所以先求个二分匹配即可,然后如果此时还是不够的话,那么剩下的猴子每只猴子都需要一条边。

如果用二分匹配的算法可能不太行,虽然看别人用Hopcroft-Carp算法跑了998ms惊险过了,不过我当时也用了Hopcroft-Carp算法。。但结果无限TLE。。。

因为模型是树,所以直接dfs求最大匹配或者树形dp求即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn=+; int n, k;
int tot;
int ans;
int head[maxn]; struct node
{
int v,next;
}e[maxn*]; namespace fastIO {
#define BUF_SIZE 1000000
//fread -> read
bool IOerror = ;
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if(p1 == pend) {
p1 = buf;
pend = buf + fread(buf, , BUF_SIZE, stdin);
if(pend == p1) {
IOerror = ;
return -;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline 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; bool used[maxn]; void addEdge(int u, int v)
{
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
} void dfs(int u, int fa)
{
int cnt=,us=;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==fa) continue;
dfs(v,u);
cnt++; //有多少个直接相连的子节点
us+=used[v]; //直接相连的子节点中有多少个已经被使用
}
if(cnt-us>=) ans++,used[u]=true;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
read(T);
while(T--)
{
tot=;
memset(head,-,sizeof(head));
read(n);
read(k);
for(int i=;i<=n;i++)
{
int x;
read(x);
addEdge(x,i);
addEdge(i,x);
}
ans=;
memset(used,false,sizeof(used));
dfs(,-);
if(*ans>=k) printf("%d\n",(k+)/);
else printf("%d\n",ans+k-*ans);
}
return ;
}

HDU 6178 Monkeys(树上的二分匹配)的更多相关文章

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

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

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

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

  3. HDU 2063 过山车(二分匹配入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...

  4. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  5. hdu 4619 Warm up 2 (二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不 ...

  6. HDU 2063 过山车 二分匹配

    解题报告:有m个女生和n个男生要结成伴坐过山车,每个女生都有几个自己想选择的男生,然后要你确定最多能组成多少对组合. 最裸的一个二分匹配,这是我第一次写二分匹配,给我最大的感受就是看那些人讲的匈牙利算 ...

  7. hdu 1528 Card Game Cheater (二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. hdu 1068 Girls and Boys (二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU - 1068 Girls and Boys(二分匹配---最大独立集)

    题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...

随机推荐

  1. PAT 1024 Palindromic Number[难]

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  2. openssl version 查看openssl 版本出现openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory,怎么办

    查看openssl版本, 解决办法: ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/li ...

  3. 机器学习理论基础学习16---高斯网络(GN)

    一.高斯网络(高斯图模型)总体介绍 概率图模型分为三种:贝叶斯网络,马尔科夫随机场以及高斯网络:而高斯网络又可以根据有向无向细分为高斯贝叶斯网络和高斯马尔科夫随机场 二.高斯贝叶斯网络 1.高斯贝叶斯 ...

  4. 测试常用的Oracle11G 命令行指令。

    测试常用的Oracle11G 命令行指令. ×××××××××××××××× 登录:

  5. 使用 MtVerify.h头文件 ,用的时候把他头文件的内容添加到项目

    #include <windows.h>  //windodws变量相关头文件 MtVerify.h的内容如下:#pragma comment( lib, "USER32&quo ...

  6. c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  7. Django初级手册5-自动化测试

    什么是自动化测试 每次更新完系统后,可自动进行测试,而不是手工从头测试一遍: 从长远和全局的角度看,测试能节约我们的时间: 测试是一种积极的行为,它能预防问题,而不仅仅是识别问题: 测试有助于代码美观 ...

  8. chrome浏览器使用

    1.如何打开多个历史网页.这个需求是这样的,有时候开了多个网页查找资料,但是又还没有做完,然后又需要重启电脑.显然重启电脑后再开启浏览器,一般都是显示浏览器的主页了,上次开的那些网页全部在历史记录里面 ...

  9. 常用jquery记录

    1.jquery easing jQuery Easing是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动.幻灯片切换等场景应用比较多.它非常小巧,且有多种动画方案供选择,使用 ...

  10. css3实现头像旋转360度

    css样式: .div a img{ width: 88px; height: 88px; border-radius: 88px; transition: all 1.2s ease-out 0s; ...