The treasure of the little Volcano income multi-school training 2 (small volcano session) POJ (adjacency table +dfs)

Source: Internet
Author: User

http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1907

Description into    The treasure, the small volcano found the treasure has n rooms, and this n rooms through the 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 <= 50), representing a total of T-group data. For each set of data, the first line is two number n, s (1 <= n <= 10000, 1 <= S <= N), n for n rooms, s for the small volcano in the beginning of the treasure after the room (small volcano How to enter the beginning of the room is not important), the second row is N number, representing each room treasure Value, followed by N-1 line, two number a per line, B, representing A, b 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 1203 5 3Sample Output206

  

Analysis: The beginning of a more tangled in the end how to deal with the room treasure other room door closed this point. Later figured out.

For example, take a point, and add the adjacent points together with the value of the point itself to compare, which big take which. (simple example, the point adjacent to it has no other adjacent points)

If there are other points adjacent to the point, then the method is still the same, according to the method of recursion.

#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<algorithm>#include<map>#include<queue>#include<stack>#include<math.h>using namespacestd;#defineMet (A, b) memset (A, B, sizeof (a))#defineMAXN 11000#defineINF 0x3f3f3f3fConst intMOD = 1e9+7; typedefLong LongLL;intCNT, HEAD[MAXN], V[MAXN], VALUE[MAXN];structnode{intu, V, next;} maps[4*MAXN];voidADD (intUintv) {MAPS[CNT].V=v; Maps[cnt].next=Head[u]; Head[u]= cnt + +;}intDFS (ints) {    intsum =0;  for(intI=head[s]; i!=-1; I=Maps[i].next) {        intp =maps[i].v; if(!V[p]) {V[p]=1; Sum+=DFS (P); }    }        returnmax (sum, Value[s]);}intMain () {intT, A, B, S, N; scanf ("%d", &T);  while(T--) {scanf ("%d%d", &n, &s);  for(intI=1; i<=n; i++) scanf ("%d", &Value[i]); memset (Head,-1,sizeof(head)); CNT=0;  for(intI=1; i<n; i++) {scanf ("%d%d", &a, &b);            Add (A, b);        ADD (b, a); } memset (V,0,sizeof(v)); V[s]=1; intAns =DFS (s); printf ("%d\n", Max (ans, value[s]); }    return 0;}/*5 2 1 Wuyi 4*/
View Code

The treasure of the little Volcano income multi-school training 2 (small volcano session) POJ (adjacency table +dfs)

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.