HDU 6178 Monkeys【dfs】【输入外挂模板】
来源:互联网 发布:ubuntu怎么连接服务器 编辑:程序博客网 时间:2024/06/01 09:13
Monkeys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 717 Accepted Submission(s): 232
Problem Description
There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occupied by at most one monkey. They want to remove some edges and leave minimum edges, but each monkey must be connected to at least one other monkey through the remaining edges.
Print the minimum possible number of remaining edges.
Print the minimum possible number of remaining edges.
Input
The first line contains an integer T (1 <= T <= 100), the number of test cases.
Each test case begins with a line containing two integers N and K (2 <= K <= N <= 100000). The second line contains N-1 space-separated integersa1,a2,…,aN−1 , it means that there is an edge between vertex ai and vertex i+1 (1 <= ai <= i).
Each test case begins with a line containing two integers N and K (2 <= K <= N <= 100000). The second line contains N-1 space-separated integers
Output
For each test case, print the minimum possible number of remaining edges.
Sample Input
24 41 2 34 31 1 1
Sample Output
22
Source
2017 Multi-University Training Contest - Team 10
题意:给一棵树,有N个节点,树上有K个猴子,每个节点最多有一个猴子,从树上的边中去掉一些边,留下尽可能少的边使得每个猴子都至少和一个其他猴子相连,求最少留的边数。
思路:从叶子节点往上配对,看有多少组可以配成两对,如果要放得猴子大于对数,则没多一个猴子则多一条边,否则直接为猴子能配成的对数。
ps(此题要使用输入外挂,要不然会超时,这个输入外挂要用freopen输入)
#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define ms(x,y) memset(x,y,sizeof(x))using namespace std;typedef long long ll;const double pi = acos(-1.0);const int mod = 1e9 + 7;const int maxn = 1e5 + 5;/*------- 开挂 -------*/ //注意此输入外挂输入要用freopennamespace fastIO { #define BUF_SIZE 100000 // fread -> read bool IOerror = 0; char nc() { static char buf[BUF_SIZE], *pl = buf + BUF_SIZE, *pr = buf + BUF_SIZE; if(pl == pr) { pl = buf; pr = buf + fread(buf, 1, BUF_SIZE, stdin); if(pr == pl) { IOerror = 1; return -1; } } 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 - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0'); } #undef BUF_SIZE};using namespace fastIO;/*------- 完结 -------*/struct Edge{ int to,next; }edge[maxn*2];int head[maxn],e;bool vis[maxn];int ans;void addedge(int u,int v){ edge[e].to=v; edge[e].next=head[u]; head[u]=e++;}void dfs(int u,int fa){ for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v==fa) continue; dfs(v,u); if(!vis[v]) { if(!vis[u]) { vis[u]=vis[v]=1; ans++; } } }}void init(){ ms(head,-1); ans=0; ms(vis,0); e=0;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; //scanf("%d",&t); read(t); while(t--) { init(); int n,m; //scanf("%d%d",&n,&m); read(n); read(m); for(int i=1;i<n;i++) { int p; //scanf("%d",&p); read(p); addedge(p,i+1); addedge(i+1,p); } dfs(1,0); if(m>=ans*2) { printf("%d\n",ans+m-2*ans); } else printf("%d\n",(m-1)/2+1); } return 0;}
阅读全文
0 0
- HDU 6178 Monkeys【dfs】【输入外挂模板】
- HDU 6178 Monkeys【DFS+输入输出外挂】
- hdu 6178 Monkeys dfs匹配
- hdu 6178 Monkeys (dfs+FastIO)
- HDU 6178 Monkeys (贪心+输入挂)
- HDU 6178 && 2017 多校训练:Monkeys(DFS)
- hdu 6178 Monkeys
- HDU-6178 Monkeys
- hdu 6178 Monkeys 贪心
- 输入外挂~模板记录
- hdu-6178-Monkeys(fast IO)
- hdu 6178 Monkeys (贪心)
- 【多校训练】hdu 6178 Monkeys 贪心+dfs+读入挂模版
- HDU6178 Monkeys【DFS】
- 输入外挂模板(适用于正负数)
- Hdu 6178 Monkeys【树形Dp-最小点覆盖+贪心思维】
- HDU 6178 Monkeys(树上的二分匹配+fread)
- HDU 6178 Monkeys(树上DP 17多校第十场)
- NSPredicate(谓语),验证邮箱、电话等用法(1)
- Android输入系统简述
- 机器学习笔记(一)
- Java NIO系列教程(五) 通道之间的数据传输
- 随笔
- HDU 6178 Monkeys【dfs】【输入外挂模板】
- Java验证码的编写
- Ionic3取消滚动条 --记录
- C++合并两个有序的单链表
- 常用简单算法
- UVA
- echarts 修改legend样式(矩形、圆、默认等)
- Linux环境下后台一直运行程序不退出的方法
- 程序精简第一步之百鸡问题(减少循环提高运行速度)