Game

Time Limit:1500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2015-05-26)

Description

It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play $k$ games simultaneously.

One day he gets a new gal game named ''XX island''. There are $n$ scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending scenes. Each scene has a value , and we use $w_i$ as the value of the $i$-th scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get $w_i$ for only once.

For his outstanding ability in playing gal games, Katsuragi is able to play the game $k$ times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for $k$ times.

 

Input

The first line contains an integer $T$($T \le 20$), denoting the number of test cases.

For each test case, the first line contains two numbers $n, k(1 \le k \le n \le 100000)$, denoting the total number of scenes and the maximum times for Katsuragi to play the game ''XX island''.

The second line contains $n$ non-negative numbers, separated by space. The $i$-th number denotes the value of the $i$-th scene. It is guaranteed that all the values are less than or equal to $2^{31} - 1$.

In the following $n - 1$ lines, each line contains two integers $a, b(1 \le a, b \le n)$, implying we can transform from the $a$-th scene to the $b$-th scene.

We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree).

 

Output

For each test case, output ''Case #t:'' to represent the $t$-th case, and then output the maximum total value Katsuragi will get. 
 

Sample Input

2
5 2
4 3 2 1 1
1 2
1 5
2 3
2 4
5 3
4 3 2 1 1
1 2
1 5
2 3
2 4
 

Sample Output

Case #1: 10
Case #2: 11
 
 
题目大意:给你一棵树,有n个结点,有n-1条边,每个结点都有一个价值,可以从跟到叶子k次,但是只能拿一次价值,问你从根1到达叶子的最大价值。
 
解题思路:用深搜划分出与根临接的顶点个数数量的子树,回溯的时候从这些子树中可以找出重链,如果在中间有不是重链上的链,则把该链放入优先队列中,否则维护出一条重链。dp[i]表示从叶子到i结点的最大价值和。
 
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define LL __int64
const int maxn=1e5+100;
vector<LL>V[maxn];
priority_queue<LL>Q;
LL a[maxn];
LL dp[maxn];
LL dfs(int cur){
int i,v;
LL tmp;
dp[cur]=a[cur];
for(i=0;i<V[cur].size();i++){
v=V[cur][i];
tmp=dfs(v);
if(dp[cur]>tmp+a[cur]){ //如果有其他轻链,把轻链放入队列
Q.push(tmp);
}else{ //如果当前这个链不是最重的链,把从叶子到该结点的这段链放入队列
Q.push(dp[cur]-a[cur]);
dp[cur]=tmp+a[cur];
}
}
return dp[cur];
}
int main(){
int t ,n,m,i,j,k,u,v,cnt=0;
LL ans;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<n;i++){
scanf("%d%d",&u,&v);
V[u].push_back(v);
}
dfs(1);
ans=dp[1];
for(i=1;i<k;i++){
ans+=Q.top();
Q.pop();
}
printf("Case #%d: %lld\n",++cnt,ans);
while(!Q.empty())
Q.pop();
for(i=0;i<=n;i++){
V[i].clear();
}
}
return 0;
}

  

hdu 5242——Game——————【树链剖分思想】的更多相关文章

  1. HDU 5242 利用树链剖分思想进行贪心

    题目大意: 在给定带权值节点的树上从1开始不回头走到某个底端点后得到所有经过的点的权值后,这些点权值修改为0,到达底部后重新回到1,继续走,问走k次,最多能得到多少权值之和 这其实就是相当于每一次都走 ...

  2. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  3. HDU 5242 树链剖分思想的贪心

    题意及博客 树链剖分分为2步,第一次求出深度,重儿子,第二次求出重链,用到了启发式的思想,即对于比较重的儿子,尽量去完整的维护它.类似于我们去合并两个堆,明显把小的堆逐个插入大的堆中会比大的往小的插更 ...

  4. HDU 5044 (树链剖分+树状数组+点/边改查)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...

  5. HDU 3966(树链剖分+点修改+点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题目大意:营地的分布成树型.每个营地都有一些人,每次修改修改一条链上的所有营地的人数,每次查询单 ...

  6. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  7. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  8. HDU - 3966-Aragorn' Story(树链剖分+线段树)

    链接:https://vjudge.net/problem/HDU-3966 题意: Our protagonist is the handsome human prince Aragorn come ...

  9. HDU 3966 RE 树链剖分 线段树 Aragorn's Story

    题意: 给出一棵树,每个顶点上有一个权值. 操作:选择一条路径,并将路径上所有的点的权值同时加或减某个数. 查询:某个点的当前权值 分析: 树链剖分完毕后,就是简单的线段树区间更新. 提交的时候注意要 ...

  10. Tree HDU - 6547 (树链剖分,线段树)

    wls 有三棵树,树上每个节点都有一个值 ai,现在有 2 种操作: 将一条链上的所有节点的值开根号向下取整: 求一条链上值的和: 链的定义是两点之间的最短路. Input 第一行两个数 n, q 分 ...

随机推荐

  1. WinForm中DataGridView的使用(三) - 各种事件

    CellMouseDown/CellMouseUp 可获得行.列号 可用if (rowIndex >= 0 && e.Y > 4 && e.Y < ( ...

  2. [Win32::Console]Perl终端版生命游戏

    环境,WinXP/Win7  Perl 5.16 默认循环1000次,按ESC提前退出 use strict; use Term::ReadKey; use Win32::Console; use T ...

  3. python-循环(while循环、for循环)

    循环:循环会重复执行循环体里面的代码,python中循环可分为while循环和for循环. break 不管循环有没有完成,立即结束循环 continue 结束本次循环,继续进行下一次循环 一.whi ...

  4. vmware vSphere虚拟网络之标准交换机(二)

    一.标准交换机的特点: 1)只能用于物理主机 2)其他主机不能共享同一个虚拟交换机 3)不具备任何灵活性 4)每台ESXi主机都要配置一遍 二.网络图: 三.创建标准交换机: 登录web vCente ...

  5. 【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

    本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情 ...

  6. ubuntu 18.04 通过联网方式安装wine

    ubuntu 18.04 通过联网方式安装wine 1.如果是64位机器,先开启允许32位架构程序运行 sudo dpkg --add-architecture i386 2.添加元wine源码安装仓 ...

  7. 13. js延迟加载的方式有哪些

    JS延迟加载,也就是等页面加载完成之后再加载 JavaScript 文件. JS延迟加载有助于提高页面加载速度.   一般有以下几种方式:   1)defer 属性 <script src=&q ...

  8. A printf format reference page (cheat sheet)

    Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my ...

  9. vue从一个页面跳转到另一个页面并携带参数

    1.需求: 点击商场跳转到商业体列表 解决方案: 元页面: a标签中添加跳转函数 <a class="orderBtn1 sIRicon2" href="javas ...

  10. LeetCode109. 有序链表转换二叉搜索树

    109. 有序链表转换二叉搜索树 问题描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超 ...