(Zzuli) 1907 Small Volcanic Treasure proceeds

Source: Internet
Author: User

Description into the treasure, the little volcano found the treasure has n rooms, and this n rooms through n-1 Door unicom. Each room has a treasure of value for AI, but every room also has a body. If the small volcano took away the treasure of this room, then the door to other rooms will never open, that is to say, the treasure of the small volcano is not available (enter the room door is not closed, the small volcano can go back); If the little volcano does not take the treasure, but to open the door to another room,  Then the treasure of this room will disappear, and the little volcano will not get the treasure of this room. However, the small volcano already has a treasure map, know the value of the treasure of each room, now want to ask you to help small volcano calculate, he can get how much money to buy stock? Input inputs The first line is an integer t (t<= -), which represents a total of T-group data. For each set of data, the first row is two numbers n, S (1<= N <=10000,1<= S <=n ), n means there are n rooms, s represents the small volcano into the treasure after the beginning of the room (small volcano How to enter the beginning of the room is not important), the second row is n number, representing the value of each room treasure, and then N-1 lines, two numbers a per line, B, for a, b these two rooms unicom. Output outputs an integer for each set of data that represents the maximum amount of money a small volcano can receive. Sample Input21 1 -3 14 5 61 22 3Sample Output -6

Test instructions from a room to enter, the treasure of the room if you take the door to other doors will be closed, but do not take, the treasure will disappear, give you a map, ask to get the maximum number of treasures.

Method from this point to search, take a road to the end, select the maximum value of the road, all roads are finished, see choose this room or the treasure on the road.

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<math.h>#include<queue>#definell Long Long#defineMet (A, b) memset (A,b,sizeof (a));#defineN 10010using namespacestd;structnode{intU,v,next;
}map[n<<2];intK;intS[n],vis[n],a[n];voidAddintUintv) {map[k].u=u; MAP[K].V=v; Map[k].next=A[u]; A[u]=k++;}intDfsinth) { intsum=0; VIS[H]=1; for(inti=a[h];i!=-1; i=Map[i].next) { intv=map[i].v; if(!Vis[v]) {Vis[v]=1; Sum+=Dfs (v); } } returnMax (s[h],sum);}intMain () {intt,n,m,e,f; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&S[i]); K=0; Met (A,-1); for(intI=1; i<n;i++) {scanf ("%d%d",&e,&f); Add (e,f); Add (f,e); } Met (Vis,0); printf ("%d\n", DFS (m)); } return 0;}

(Zzuli) 1907 Small Volcanic Treasure proceeds

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.