Broadcast Stations

Given a tree, assign non-negative integer powers to vertices so every zero-power vertex lies within reach of some positive-power vertex, minimizing the total power. Report that minimum sum.

Hard8Dynamic programmingTreeGreedyDFSNo attempts yetTime limit0.5sMemory limit512 MB

Problem

The connections between cities form a tree TT with nn vertices, and each vertex is one city. The distance between two vertices is the number of edges on the unique path between them. Broadcast stations that send out the same information have to be built in some of the cities. A station of transmission power pp reaches every city at distance pp or less from it.

Assign a non-negative integer p(v)p(v), called the broadcast power, to every vertex vv of the vertex set VV of TT. The assignment has to satisfy the following condition: every vertex uu with p(u)=0p(u) = 0 is within distance p(v)p(v) of some vertex vv with p(v)>0p(v) > 0. A vertex vv with p(v)>0p(v) > 0 is a broadcast station of transmission power p(v)p(v), and a vertex uu with p(u)=0p(u) = 0 can hear the broadcast of vv if uu is within distance p(v)p(v) of vv.

Among the assignments that satisfy the condition, find the minimum value of vVp(v)\sum_{v \in V} p(v).

Figure A.1 shows two assignments of broadcast powers. In (a) only vertex 6 has broadcast power 4 and every other vertex has 0. Every vertex of broadcast power 0 can then hear vertex 6. In (b) vertices 3 and 9 have broadcast powers 2 and 1. Every vertex of broadcast power 0 can hear vertex 3 or vertex 9, and this assignment makes the sum of the broadcast powers as small as possible.

Figure A.1 (a)

Figure A.1 (b)

Figure A.1: two assignments of broadcast powers, (a) above and (b) below.

Input

The first line contains one integer nn (1n50001 \le n \le 5000), the number of vertices of the tree TT. The vertices are numbered from 1 to nn. Each of the next n1n-1 lines contains two integers aa and bb (1a,bn1 \le a, b \le n), an edge that joins vertex aa and vertex bb.

Output

Print one line with one integer, the smallest sum of broadcast powers over all assignments that satisfy the condition.