[Solution] Triameter Codeforces Solution | Solution Codeforces
F. Triameter
time limit per test
4.5 secondsmemory limit per test
768 megabytesinput
standard inputoutput
standard outputA tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.
You are given a weighted tree with vertices, each edge has a weight of . Let be the set of vertices with degree equal to .
You have to answer independent queries. In the -th query:
- You are given a positive integer .
- For all such that , add edge with weight to the graph (initially the given tree).
- Find the diameter of the resulting graph.
The diameter of a graph is equal to , where is the length of the shortest path
👇👇👇👇👇
between vertex and vertex .
Input
The first line contains a single integer ().
The second line contains integers () indicating that there is an edge between vertices and . It is guaranteed that the given edges form a tree.
The third line contains a single integer ().
The fourth line contains integers (). All are distinct.
Output
Print integers in a single line — the answers to the queries.
No comments:
Post a Comment