Minimum cost to color a tree
InterviewTime limit1sMemory limit512 MB
Given a tree rooted at 0 with white and black costs per vertex, find the minimum total cost so that every adjacent pair has different colors.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Problem
There is a tree with vertices and edges. Vertex numbers run from 0 to , and vertex 0 is the root. The edges have no weights.
You want to color each vertex of either white or black. Two adjacent vertices must have different colors. Each vertex has a cost for coloring it white and a cost for coloring it black.
Find the minimum total cost to color all vertices of .
Input
The first line contains the number of vertices .
Each of the next lines contains one edge as two integers, the parent vertex and the child vertex , separated by a space.
Each of the next lines gives the costs for one vertex, in order from vertex 0 to vertex . Each line has two integers and , separated by a space. is the cost to color the vertex white, and is the cost to color it black.
Output
Print the minimum cost to color all vertices of tree on the first line.
Constraints
- ,
- The edges form a tree.
- , and and are positive integers.