This page is still under construction.

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

Visiting Cows

Interview

Time limit1sMemory limit128 MB

Summary
Given a tree with N vertices, choose the largest set of vertices with no two adjacent, which is the maximum independent set on a tree.
Level

Medium6 of 10

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

Problem

After many weeks of hard work, Bessie is finally getting a vacation! Being the most social cow in the herd, she wishes to visit her NN (1≤N≤500001 \le N \le 50000) cow friends, conveniently numbered 1…N1 \ldots N.

The cows have set up an unusual road network with exactly N−1N-1 roads. Each road connects a pair of cows C1C_1 and C2C_2 (1≤C1≤N1 \le C_1 \le N, 1≤C2≤N1 \le C_2 \le N, C1≠C2C_1 \ne C_2), and there is a unique path of roads between any two cows. In other words, the road network forms a tree.

Farmer John wants Bessie to come back to the farm soon, so he has told her that if two cows are directly connected by a road, she may not visit them both. Of course, Bessie would like her vacation to be as long as possible, so she wants to determine the maximum number of cows she can visit.

Input

  • Line 1: A single integer NN.
  • Lines 2 to NN: Each line describes one road with two space-separated integers C1C_1 and C2C_2.

Output

  • Line 1: A single integer, the maximum number of cows that Bessie can visit.

Hint

Bessie knows 7 cows. Cows 6 and 2 are directly connected by a road, as are cows 3 and 4, cows 2 and 3, and so on. The illustration below shows the roads that connect the cows:

1--2--3--4
   |
5--6--7

Bessie can visit four cows. One of the best combinations is two cows from the top row and two from the bottom row. She cannot visit cow 6, because that would prevent her from visiting cows 5 and 7; so she visits cows 5 and 7 instead. From the top row she can visit any of {1, 3}, {1, 4}, or {2, 4}.

Examples1

  1. Example 1

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