CF1712F.Triameter
NOI/NOI+/CTSC
通过率:0%
时间限制:4.50s
内存限制:768MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
— What is my mission?
— To count graph diameters.
You and Your Submission
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:
- You are given a positive integer xi.
- For all u,v∈L such that u<v, add edge (u,v) with weight xi to the graph (initially the given tree).
- Find the diameter of the resulting graph.
The diameter of a graph is equal to 1≤u<v≤nmaxd(u,v), where d(u,v) is the length of the shortest path between vertex u and vertex v.
— 我的任务是什么?
— 计算图的直径。
你与你的提交
树是一种无环的连通无向图。带权树为每条边赋予一个权重。顶点的度数是指与该顶点相连的边的数量。
给定一棵含 n 个顶点的带权树,其中每条边的权重均为 1。令 L 表示所有度数为 1 的顶点构成的集合。
你需要回答 q 个相互独立的查询。在第 i 个查询中:
- 给定一个正整数 xi;
- 对所有满足 u,v∈L 且 u<v 的顶点对,在图中(初始为给定的树)添加一条权重为 xi 的边 (u,v);
- 求所得图的直径。
图的直径定义为 1≤u<v≤nmaxd(u,v),其中 d(u,v) 表示顶点 u 与顶点 v 之间最短路径的长度。
输入格式
The first line contains a single integer n (3≤n≤106).
The second line contains n−1 integers p2,p3,…,pn (1≤pi<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 (1≤q≤10).
The fourth line contains q integers x1,x2,…,xq (1≤xi≤n). All xi are distinct.
第一行包含一个整数 n(3≤n≤106)。
第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi<i),表示顶点 i 与顶点 pi 之间存在一条边。保证所给的边构成一棵树。
第三行包含一个整数 q(1≤q≤10)。
第四行包含 q 个整数 x1,x2,…,xq(1≤xi≤n)。所有 xi 互不相同。
输出格式
Print q integers in a single line — the answers to the queries.
在一行中输出 q 个整数——各查询的答案。
输入输出样例
输入#1
4 1 2 2 4 1 2 3 4
输出#1
1 2 2 2
输入#2
7 1 2 3 4 2 1 7 2 1 3 7 5 6 4
输出#2
3 3 4 5 5 5 4
输入#3
3 1 2 1 1
输出#3
1
说明/提示
The graph in the first test after adding the edges:

添加边后第一个测试用例的图:

输入解题思路,AI测评打分。不知道怎么写?