Farthest Vertex

AtCoder
IDabc428_e
Time2000ms
Memory256MB
Difficulty
There is a tree with $N$ vertices numbered $1$ to $N$. The $i$\-th edge connects vertices $A_i$ and $B_i$. Define the distance between vertices $u$ and $v$ as the number of edges in the path with endpoints at vertices $u$ and $v$. (This path is uniquely determined.) Solve the following problem for $v = 1, 2, \dots, N$. * Among vertices $1, 2, \dots, N$, output the number of the vertex that has the maximum distance from vertex $v$. If there are multiple vertices that satisfy the condition, output **the vertex with the largest number**. ## Constraints * $2 \leq N \leq 5 \times 10^5$ * $1 \leq A_i \lt B_i \leq N$ * The graph given in the input is a tree. * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $A_1$ $B_1$ $A_2$ $B_2$ $\vdots$ $A_{N-1}$ $B_{N-1}$ [samples]
Samples
Input #1
3
1 2
2 3
Output #1
3
3
1

The vertex with the maximum distance from vertex $1$ is vertex $3$.  
The vertices with the maximum distance from vertex $2$ are vertices $1$ and $3$. Among them, vertex $3$, which has the larger number, is the answer.  
The vertex with the maximum distance from vertex $3$ is vertex $1$.
Input #2
5
1 2
2 3
2 4
1 5
Output #2
4
5
5
5
4
API Response (JSON)
{
  "problem": {
    "name": "Farthest Vertex",
    "description": {
      "content": "There is a tree with $N$ vertices numbered $1$ to $N$. The $i$\\-th edge connects vertices $A_i$ and $B_i$.   Define the distance between vertices $u$ and $v$ as the number of edges in the path with en",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc428_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There is a tree with $N$ vertices numbered $1$ to $N$. The $i$\\-th edge connects vertices $A_i$ and $B_i$.  \nDefine the distance between vertices $u$ and $v$ as the number of edges in the path with en...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments