CF1140G.Double Tree
省选/NOI-
通过率:0%
时间限制:10.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a special undirected graph. It consists of 2n vertices numbered from 1 to 2n. The following properties hold for the graph:
- there are exactly 3n−2 edges in the graph: n edges connect vertices having odd numbers with vertices having even numbers, n−1 edges connect vertices having odd numbers with each other, and n−1 edges connect vertices having even numbers with each other;
- for each edge (u,v) between a pair of vertices with odd numbers, there exists an edge (u+1,v+1), and vice versa;
- for each odd number u∈[1,2n−1], there exists an edge (u,u+1);
- the graph is connected; moreover, if we delete all vertices with even numbers from it, and all edges incident to them, the graph will become a tree (the same applies to deleting odd vertices).
So, the graph can be represented as two trees having the same structure, and n edges connecting each vertex of the first tree to the corresponding vertex of the second tree.
Edges of the graph are weighted. The length of some simple path in the graph is the sum of weights of traversed edges.
You are given q queries to this graph; in each query, you are asked to compute the length of the shortest path between some pair of vertices in this graph. Can you answer all of the queries?
你被给定一个特殊的无向图。该图包含 2n 个顶点,编号从 1 到 2n。该图满足以下性质:
- 图中恰好有 3n−2 条边:其中 n 条边连接奇数编号顶点与偶数编号顶点,n−1 条边连接奇数编号顶点彼此之间,另外 n−1 条边连接偶数编号顶点彼此之间;
- 对于任意一条连接两个奇数编号顶点 u 与 v 的边 (u,v),图中必存在边 (u+1,v+1),反之亦然;
- 对每个奇数 u∈[1,2n−1],图中均存在边 (u,u+1);
- 图是连通的;此外,若删除图中所有偶数编号顶点及其所有关联边,则剩余部分构成一棵树(对奇数编号顶点做同样操作,剩余部分也是一棵树)。
因此,该图可表示为两棵结构完全相同的树,外加 n 条边,每条边连接第一棵树中的某个顶点与其在第二棵树中对应的顶点。
图中的边均带有权重。某条简单路径的长度定义为该路径上所有经过边的权重之和。
你将收到关于该图的 q 个查询;对每个查询,你需要计算图中某一对顶点之间的最短路径长度。你能回答所有这些查询吗?
输入格式
The first line of the input contains one integer n (2≤n≤3⋅105).
The second line contains n integers w1,2, w3,4, ..., w2n−1,2n (1≤wi,i+1≤1012). These integers describe the weights of the edges connecting odd vertices with even ones.
Then n−1 lines follow. i-th line contains four integers xi, yi, wi,1 and wi,2 (1≤xi,yi≤n, xi=yi, 1≤wi,j≤1012); it describes two edges: one connecting 2xi−1 with 2yi−1 and having weight wi,1; another connecting 2xi with 2yi and having weight wi,2.
The next line contains one integer q (1≤q≤6⋅105) — the number of queries.
Then q lines follow, i-th line contains two integers ui and vi (1≤ui,vi≤2n, ui=vi), describing a query "compute the length of the shortest path between vertices ui and vi".
输入的第一行包含一个整数 n(2≤n≤3⋅105)。
第二行包含 n 个整数 w1,2、w3,4、…、w2n−1,2n(1≤wi,i+1≤1012)。这些整数表示连接奇数编号顶点与偶数编号顶点的边的权重。
接下来是 n−1 行。第 i 行包含四个整数 xi、yi、wi,1 和 wi,2(1≤xi,yi≤n,xi=yi,1≤wi,j≤1012);它描述两条边:一条连接顶点 2xi−1 与 2yi−1,权重为 wi,1;另一条连接顶点 2xi 与 2yi,权重为 wi,2。
下一行包含一个整数 q(1≤q≤6⋅105)—— 查询的数量。
随后是 q 行,第 i 行包含两个整数 ui 和 vi(1≤ui,vi≤2n,ui=vi),表示一个查询:“计算顶点 ui 与 vi 之间最短路径的长度”。
输出格式
Print q integers, i-th integer should be equal to the answer to the i-th query.
输出 q 个整数,其中第 i 个整数应等于第 i 个查询的答案。
输入输出样例
输入#1
5 3 6 15 4 8 1 2 5 4 2 3 5 7 1 4 1 5 1 5 2 1 3 1 2 5 6 1 10
输出#1
3 15 4
说明/提示
The graph in the first test looks like that:

第一个测试用例中的图如下所示:

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