Harvesting apples and pears from k tree nodes
InterviewTime limit3sMemory limit512 MB
Pick a connected set of at most k nodes containing root 0 to maximize apples times pears, then print the apple and pear counts.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Problem
A tree has nodes and edges. The nodes are numbered from 0 to , and node 0 is the root. The edges have no weights. Each node in has one apple, has one pear, or has neither. Starting from the root, you visit neighboring nodes to harvest apples and pears. When you visit at most nodes, output the number of apples and the number of pears that maximize (number of apples) (number of pears). If several pairs give the maximum product, output the pair with more apples. If several pairs have the same maximum product and the same number of apples, output the pair with more pears. A node visited more than once counts as visited once. The root counts as visited. Even if you visit a node with an apple or a pear several times, you can harvest only one apple or pear from it, on the first visit.
Input
The first line contains the number of nodes and the integer , separated by a space.
The next lines each contain one edge. Each edge is given as a parent node number and a child node number , separated by a space.
The next line contains integers, separated by spaces. The -th integer gives the information of node , for nodes 0 to . A value of 0 means the node has neither an apple nor a pear. A value of 1 means the node has one apple only. A value of 2 means the node has one pear only.
Output
On the first line, print the number of apples and the number of pears that satisfy the conditions of the problem, separated by a space.
Constraints
- ,
- The graph formed by the edges is a tree.
- The information of each node is 0, 1, or 2.