GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Tuesday 16 August 2022

[Solution] Triameter Codeforces Solution | Solution Codeforces



F. Triameter
time limit per test
4.5 seconds
memory limit per test
768 megabytes
input
standard input
output
standard output


A 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 n vertices, each edge has a weight of 1. Let L be the set of vertices with degree equal to 1.

You have to answer q independent queries. In the i-th query:

  1. You are given a positive integer xi.
  2. For all u,vL such that u<v, add edge (u,v) with weight xi to the graph (initially the given tree).
  3. Find the diameter of the resulting graph.

The diameter of a graph is equal to max1u<vnd(u,v), where d(u,v) is the length of the shortest path

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

 between vertex u and vertex v.

Input

The first line contains a single integer n (3n106).

The second line contains n1 integers p2,p3,,pn (1pi<i) indicating that there is an edge between vertices i and pi. It is guaranteed that the given edges form a tree.

The third line contains a single integer q (1q10).

The fourth line contains q integers x1,x2,,xq (1xin). All xi are distinct.

Output

Print q integers in a single line — the answers to the queries.

No comments:

Post a Comment