Hdoj 5242 Game

Source: Internet
Author: User


GameTime limit:3000/1500 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 584 Accepted Submission (s): 170


Problem DescriptionIt is well known that Keima Katsuragi are the capturing God because of his exceptional skills and experi ence 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 is n Scenes in that game, and one scene would 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 was exactly the opening scene while leaves be all th E ending scenes. Each scene have a value, and we use w i As the value of the i -th scene. Once Katsuragi Entering some new scene, he'll get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he'll get w i for only once.

For he outstanding ability in playing gal games, Katsuragi are able to play the game k Times simultaneously. Now is asked to calculate the maximum total value he'll get by playing that game for k Times.

Inputthe first line contains an integer T ( T≤ ), denoting the number of test cases.

For each test case, the first line contains the numbers n k ( 1 ≤ k ≤ n ≤ 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, all the values were less than or equal to 2? 1 .

In the following n? 1 Lines, each line contains the integers a,b(1≤a,b ≤ 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).


Outputfor each test case, output "case #t:" to represent the t -th case, and then the output of the maximum total value Katsuragi would get.

Sample Input
25 24 3 2 1 11 21 52 32 45 34 3 2 1 11 21 52 32 4

Sample Output
Case #1:10Case #2:11

Sourcethe ACM-ICPC China Shanghai Metropolitan Programming Contest


/* ***********************************************author:ckbosscreated time:2015 June 07 Sunday 16:39 51 seconds file Name : hdoj5239.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef Long long int ll;const int maxn=100100;int n,m;struct edge{int to,next;} Edge[maxn*2];int adj[maxn],size;void init () {memset (adj,-1,sizeof (ADJ)); size=0;} void Add_edge (int u,int v) {edge[size].next=adj[u];edge[size].to=v; adj[u]=size++;} LL VAL[MAXN],SUMV[MAXN];p riority_queue<ll> q; ll dfs (int u,int fa) {ll pos=0;for (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to;if (V==FA) Continue;sumv[v]=dfs (v,u ); if (Sumv[v]>sumv[pos]) pos=v;} for (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to;if (v==pos| | V==FA) Continue;q.push (Sumv[v]);}sumv[u]=val[u]+sumv[pos];return Sumv[u];    }int Main () {//freopen ("In.txt", "R", stdin);//freopen ("OUT.txt", "w", stdout); int t_t,cas=1;scanf ("%d", &t_t), while (t_t--) {scanf ("%d%d", &n,&m); init (); for (int i=1;i<=n;i++) { scanf ("%i64d", Val+i);} for (int i=0,u,v;i<n-1;i++) {scanf ("%d%d", &u,&v); Add_edge (U,V); Add_edge (v,u);} while (!q.empty ()) Q.pop ();d FS (), Q.push (sumv[1]); LL Ans=0;while (!q.empty () &&m--) {ans + = q.top (); Q.pop ();} printf ("Case #%d:%i64d\n", Cas++,ans); } return 0;}


Hdoj 5242 Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.