Minequake

면접 대비

시간 제한2초메모리 제한1024 MB

요약
트리에서 모든 정점을 방문하는 가장 짧은 경로를 찾아, 방문 시간의 합을 최소화하는 문제입니다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법
정답자
아직 제출이 없습니다

문제

The fully autonomous microbreweries installed in the abandoned Dwarven mines of Moravia are truly a testament to the ingenuinity and craftsmanship of Dwarven engineering! Alas, sometimes earthquakes rattle the mines, leading to misaligned pipes and funnels spilling precious liquid on the floor. As the Exalted Warden of Brewery Safety it is your responsibility to turn off the machines in every hall in case of an earthquake.

Walking through tunnels takes time, so you will inevitably arrive late at many of the machines. This cannot be avoided, but you want to minimise the total amount of spilled liquid.

The Dwarven mines consist of nn halls connected by n−1n-1 tunnels. The entire system is connected, so it is possible to get from any hall to any of the others. It takes 11 unit of time to traverse a tunnel. Switching off a machines and traversing a hall takes no time. In each hall, turning off the machines at time tt after the earthquake spills tt liters of liquid. There is exactly one earthquake, the earthquake affects all halls at the same time, and you may not switch off any machines before the earthquake. You can start in any of the halls.

In sample input 11, the mines look like this:

If you start in hall 22 and visit the rest of the halls in the order 22, 11, 22, 33, then you can switch off their machines at time 00 (in hall 22), time 11 (in hall 11), and time 33 (in hall 33). This wastes 0+1+3=40+1+3=4 liters of liquid in total. If instead you start in hall 11 and visit the halls in the order 11, 22, 33, the total amount of liquid wasted is 0+1+2=30+1+2=3 liters, which is better.

입력

The first line of input consists of the integer nn, denoting the number of halls. We assume that the halls are numbered 11, …\ldots, nn. The next n−1n-1 lines each contain two space-separated integers uu and vv with 1≤u<v≤n1\leq u < v \leq n, meaning that there is a tunnel between hall uu and hall vv.

출력

Print a single integer: the minimum amount of spilled liquid, in liters.

제한

We always have 1≤n≤1051\leq n\leq 10^5.

예제3

  1. 예제 1

    입력
    3
    1 2
    2 3
    
    예상 출력
    3
    
  2. 예제 2

    입력
    4
    1 2
    1 3
    1 4
    
    예상 출력
    7
    
  3. 예제 3

    입력
    1
    
    예상 출력
    0