Gems
InterviewTime limit1sMemory limit128 MB
Given a tree, assign positive integer prices to vertices so adjacent vertices differ, minimizing total sum, essentially a greedy coloring based on tree structure.
Problem
The Gem-Toys Company has asked you to solve the following problem.
You are given a connected acyclic graph — a tree — a set of vertices joined by edges so that every vertex can reach every other vertex, with no cycle.
The company will manufacture jewelry models shaped like such trees: each vertex is a gem and each edge is a piece of gold string. Two adjacent vertices must be made of different kinds of gems. For every positive integer there is exactly one kind of gem, and its price is .
Compute the minimum total price of the gems needed to build the model.
Input
The first line contains one integer (), the number of vertices, numbered from to .
Each of the next lines contains two integers and (, ) describing an edge between vertices and .
Output
Print a single integer: the minimum total price of the gems needed to build the model.