Distance Sum Maximization
Time limit3sMemory limit1024 MB
Given a tree, answer each query (u, v) by finding the maximum of dist(x,u) + dist(x,v) over all vertices x.
- Level
Medium7 of 10
- Topics
- Tree, Segment tree, DFS
- Solved
- No attempts yet
Problem
A tree with vertices (an undirected graph with no cycles that is connected) is given. The vertices are numbered from to , and the edges are numbered from to .
Write a program that processes the following query.
- : For a vertex (), print the maximum value of + . ()
Here is the number of edges on the shortest path from vertex to vertex . For every vertex of the tree, .
Input
The first line contains the number of vertices ().
Each of the next lines describes the tree. The -th line contains the two vertices connected by edge , separated by a space.
The next line contains the number of queries (). Each of the following lines contains one query.
Output
Print the answers to the queries in order, one per line.