Starship Troopers records

Source: Internet
Author: User

Starship Troopers

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6816 accepted submission (s): 1845


Problem descriptionyou, the leader of Starship Troopers, are sent to destroy a base of the bugs. the base is built underground. it is actually a huge cavern, which consists of specified rooms connected with tunnels. each room is occupied by some bugs, and their brains hide in some
Of the rooms. Scientists have just developed a new weapon and want to experiment it on some brains. Your task is to destroy the whole base, and capture as your brains as possible.

To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. the cavern's structure is like a tree in such a way that there is
One unique path leading to each room from the entrance. to finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight
All the bugs inside. The troopers never re-enter a room where they have visited before.

A starship trow.can fight against 20 bugs. since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. at the mean time, You shoshould maximize the possibility of capturing a brain. to simplify
Problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. making such a plan is a difficult job. You need the help of a computer.


Inputthe input contains several test cases. the first line of each test case contains two integers n (0 <n <= 100) and M (0 <= m <= 100 ), which are the number of rooms in the cavern and the number of Starship Troopers you have, respectively. the following n lines
Give the description of the rooms. each line contains two non-negative integers -- the amount of bugs inside and the possibility of containing a brain, respectively. the next n-1 lines give the description of tunnels. each tunnel is described by two integers,
Which are the indices of the two rooms it connects. Rooms are numbered from 1 and Room 1 is the entrance to the cavern.

The last test case is followed by two-1's.


Outputfor each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.


Sample Input

5 1050 1040 1040 2065 3070 301 21 32 42 51 120 7-1 -1
 


Sample output

507

Description

You, as the leader of the galaxy team, are sent to eliminate a base for a bug. The base is built underground. This is a very large cave, composed of many rooms and passages between rooms. Each room is occupied by some bugs, where the worms (cool bugs) are hidden in some specific rooms. Scientists have developed a new weapon to experiment on the brain worm. Your task is to destroy the entire base and capture as many brain worms as possible.

Eliminating all the bugs is always easier than capturing the worms. There is a map on your hand that shows the number of bugs in each room and where the brain worms may hide. The whole cave is a tree structure, and each room has only one access path to the exit. You want to quickly solve the battle, so you can't wait to clean up a room and move forward to the next room. Instead, you can leave some troops to eliminate the bugs in the room when using a room. The troops will not repeat into a room that has been accessed.

A galaxy team can eliminate 20 bugs. Because there are not enough troops, you can only select some rooms to dispatch troops, and the remaining rooms can only be cleaned by gas. At the same time, you should capture as many insects as possible. To simplify the problem, we only need to make the room destroyed by the troops contain the largest number and maximum number of brain worms. Developing such a plan is not a simple task. You need the help of a computer.

Input

The input includes multiple groups of test data. The first row contains two integers, n (0 <n <= 100) and M (0 <= m <= 100 ), the number of rooms in the base and the number of Galaxy squads you own. The next n rows describe each base room. Each line contains two non-negative integers, indicating the number of bugs in the room and the number of possible cerebral worms. Next, the N-1 line gives the description of the room channel, including two integers, indicating the number of the room connected to both ends of the channel respectively. The room number starts from 1, and room 1 is the entrance to the whole cave.
The last group of test data is composed of two to one, indicating the end.

Output

Output a row of test data in each group, including an integer, indicating the maximum number of brain worms that may be captured

Prompt

Question Source

Hard drive http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1011

Import Java. util. arraylist; import Java. util. list; import Java. util. optional;/*** for hangdian acm1011 * @ author 718225250 **/public class main {public static class cavern {integer id; int bugs; int brains; int troopers; boolean ispass = false; // whether list <integer> next = new arraylist <integer> (); Public cavern (int id, int bugs, int brains) {This. id = integer. valueof (ID); this. bugs = bugs; this. brains = brains; this. Troopers = (bugs % 20 = 0? Bugs/20: Bugs/20 + 1);}/*** get the maximum value of a traversal * @ return */public static int getmax () {int max = curmax; int id = _ list. get (_ list. size ()-1); Boolean hasnextforpass = false;/* whether there is another node for traversal */For (INT I = 0; I <_ caverns [ID]. next. size (); I ++) {INTEGER nextid = _ caverns [ID]. next. get (I); If (! _ Caverns [nextid]. ispass) {hasnextforpass = true; _ list. add (nextid); _ caverns [nextid]. ispass = true; max = getmax (); _ caverns [nextid]. ispass = false; _ list. remove (_ list. size ()-1) ;}} if (! Hasnextforpass) {int tempmax = getresult (); If (tempmax> MAX) {curmax = tempmax; max = curmax ;}} return Max ;} /*** get the result * @ return */public static int getresult () {int result = 0; int temptroopernumber = _ troopernumber; int tempresult = 0; Boolean flag = true; for (INT I = 0; I <_ list. size (); I ++) {INTEGER id = _ list. get (I); If (temptroopernumber> = _ caverns [ID]. troopers) {temptroopernumber-= _ caverns [ID]. troopers; tempresult + = _ caverns [ID]. brains;} else {flag = false; break;} If (FLAG) {result = tempresult;} else {tempresult = 0; For (INT I = 0; I <_ list. size (); I ++) {INTEGER id = _ list. get (I); _ list. remove (ID); tempresult = getresult (); If (tempresult> result) {result = tempresult;} _ list. add (ID) ;}} return result;} public static int _ cavernnumber; public static int _ troopernumber; public static cavern [] _ caverns; public static int curmax; public static final list <integer> _ list = new arraylist <integer> (); public static void main (string [] ARGs) {region in = new region (system. in); While (true) {__ cavernnumber = in. nextint () ;__ troopernumber = in. nextint (); If (_ cavernnumber <0 & _ troopernumber <0) {break ;} else if (_ cavernnumber = 0 | _ troopernumber = 0) {system. out. println (0);} else {_ caverns = new cavern [_ cavernnumber]; for (INT I = 0; I <__ cavernnumber; I ++) {int bugs = in. nextint (); int brains = in. nextint (); _ caverns [I] = new cavern (I, bugs, brains);} For (INT I = 0; I <__ CavernNumber-1; I ++) {int M = in. nextint (); int n = in. nextint (); _ caverns [M-1]. next. add (n-1); _ caverns [n-1]. next. add (m-1) ;}curmax = 0; integer start = integer. valueof (0); _ caverns [0]. ispass = true; _ list. add (start); system. out. println (getmax ());}}}}

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.