This page is still under construction.

Parts of this page are still being built. What you see may change.

Two Postmen

Interview

Time limit1sMemory limit128 MB

Summary
Split the edges of a tree rooted at node 1 between two postmen starting at the root so the later finishing time is minimized.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, DFS, Greedy
Solved
No attempts yet

Problem

A village has just opened a brand-new post office. It hired two postmen who, every morning, set out from the post office and deliver letters throughout the village. You must plan their routes so that the last letter is delivered as early as possible.

The village has nn houses numbered from 11 to nn. The post office is house 11. The houses are joined by n−1n-1 two-way roads, and this road network lets you travel between any two houses (that is, the network forms a tree). Traversing a single road takes a postman 11 minute.

Both postmen start at the post office (house 11), and every house must receive its letter. Each road only needs to be traveled by at least one of the two postmen. A postman does not need to return to the post office after finishing. The time at which the last letter is delivered is the later of the two postmen's finishing times, and the goal is to make this value as small as possible.

Input

The first line contains an integer nn, the number of houses in the village (1≤n≤30001 \le n \le 3000).

Each of the next n−1n-1 lines describes one road. A line contains two integers aa and bb, meaning there is a road connecting house aa and house bb (1≤a,b≤n1 \le a, b \le n).

Output

Print, on a single line, the minimum number of minutes in which the two postmen can deliver all the letters.

Examples2

  1. Example 1

    Input
    6
    1 2
    2 3
    5 2
    3 4
    6 1
    
    Expected output
    4
    
  2. Example 2

    Input
    5
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    4