hdu--6178(多校
题意:要在一棵 n 个点的树上放 k 只猴子,然后删掉尽量多的边,使得删边后,每只猴子都至少和另外一只猴子相连,问最后剩下的边数。
思路:其实dfs遍历一次看有多少个点-边-点就好了,比赛的时候就觉得要从树尾开始分,其实不是,dfs遍历,vis标记就好了。这题的输入很大,要用多校给过的读入挂。
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = +;
const int N = ;
const ll maxm = 1e7;
const int INF = 0x3f;
const int mod=;
const ll inf = 1e15 + ;
const db eps = 1e-;
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;
struct Edge{
int u, v, next;
}e[maxn*];
int head[maxn*], cnt, vis[maxn*];
int dan; void init() {
cnt=, dan=;
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
}
void add(int u, int v) {
e[cnt].v=v, e[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u, int fa) {
for (int i=head[u]; ~i; i=e[i].next) {
int v=e[i].v;
if (v!=fa && v!=u) {
dfs(v, u);
if (!vis[v]) {
if (!vis[u]) {
vis[u]=vis[v]=;
}
else dan++;
}
}
}
}
void solve() {
init();
int n, k;
read(n); read(k);
for (int i=; i<=n-; i++) {
int u; read(u);
add(u, i+); add(i+, u);
}
dfs(, );
if (!vis[]) dan++;
int can=(n-dan)/;
int ans=;
if (*can<k) {
ans=k-can;
}
else ans=(k+)/;
printf("%d\n", ans);
}
int main() {
int t = ;
//freopen("in.txt", "r", stdin);
//scanf("%d", &t);
read(t);
while(t--)
solve();
return ;
}
hdu--6178(多校的更多相关文章
- 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 - 5753 多校联萌3-2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5753 Sample Input Sample Output 6.000000 52.833333 分析 ...
- HDU 5358 多校第6场 First One
First One Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- hdu 6045 多校签到题目
http://acm.hdu.edu.cn/showproblem.php?pid=6045 题解:遍历一遍,求出两个人答案中相同的个数,用wa表示.然后我从大的数入手,当wa的数都尽可能在两个人答案 ...
- 【HDU 5810多校】Balls and Boxes(打表/数学)
1.打表找规律,下面是打表程序: #include <iostream> #include <cstdio> #define ll long long #define N 10 ...
- 【HDU 5818多校】Joint Stacks
用两个栈模拟,并保存每个点的时间戳.每次合并的时候记录合并时的时间戳mcnt和此时的topa和topb记做ta.tb. 每次pop的时候,如果栈的top的时间戳大于mcnt,则普通地pop,否则就在两 ...
- HDU 4635 多校第四场 1004 强联通
我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...
- HDU 4622 多校第三场1002 后缀自动机
比赛的时候我是用后缀数组的,但是T了. 赛后看了解题报告说,后缀数组貌似是卡你常数的时间,我算了下复杂度O(T * Q * n).这是10 ^ 8,但是考虑到每次询问的时候都要重新构造字符,所以那个n ...
随机推荐
- apache服务器伪静态配置说明
apache服务器伪静态配置说明: 第一种 .如果是多城市版分类并且使用了城市二级域名即(多城市+多域名),请修改apache的配置文件,把以下代码添加到配置文件的最后一行即可,注意把qibosoft ...
- x86寄存器总结
X86寄存器 ·x86寄存器分类: 8个通用寄存器:EAX.EBX.ECX.EDX.ESI.EDI.ESP.EBP 1个标志寄存器:EFLAGS 6个段寄存器:CS.DS.ES.FS.GS.SS 5个 ...
- 最长公共子序列hdu1503
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意:给你两个字符串,把这两个字符串合并,使合并之后的字符串最短,并且合并之后的字符之间的相对位 ...
- 贪吃蛇Ground Java实现(二)
package cn.tcc.snake.antition; import java.awt.Color;import java.awt.Graphics; import java.awt.Point ...
- springmvc整合mybatis 配置文件
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- PLSQL连接Oracle数据库问题及详解
一.Oracle数据库安装步骤参考:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 二.Oracle客户端安装:http ...
- NET Runtime version 2.0.50727.42 - 执行引擎错误 或者无法创建应用程序域
server2003操作系统 IIS运行应用程序报错,应用程序事件查看器详细: NET Runtime version 2.0.50727.42 - 执行引擎错误 或者无法创建应用程序域 解决方法:卸 ...
- C语言可以开发哪些项目?(转)
原文地址:https://www.cnblogs.com/shiyanlou/p/6098661.html 知乎:https://www.zhihu.com/question/20564904 C语言 ...
- 微信小程序中用setData修改一个对象的属性值
原创文章 1. Page({ data: { items:{ //items为一个对象,is_like和like分别为其两个属性 is_like: 0, like: 0 ...
- 定时器NSTimer
/** 添加定时器 */@property (nonatomic, strong) NSTimer *timer; - (void)addTimer{ // 2秒后,自己 调用nextImage方法 ...