A12557 | Double Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 \in [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?
- 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 \in [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?
输入格式
The first line of the input contains one integer $n$ ( $2 \le n \le 3 \cdot 10^5$ ).
The second line contains $n$ integers $w_{1, 2}$ , $w_{3,4}$ , ..., $w_{2n - 1, 2n}$ ( $1 \le w_{i, i + 1} \le 10^{12}$ ). 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 $x_i$ , $y_i$ , $w_{i, 1}$ and $w_{i, 2}$ ( $1 \le x_i, y_i \le n$ , $x_i \ne y_i$ , $1 \le w_{i, j} \le 10^{12}$ ); it describes two edges: one connecting $2x_i - 1$ with $2y_i - 1$ and having weight $w_{i, 1}$ ; another connecting $2x_i$ with $2y_i$ and having weight $w_{i, 2}$ .
The next line contains one integer $q$ ( $1 \le q \le 6 \cdot 10^5$ ) — the number of queries.
Then $q$ lines follow, $i$ -th line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le 2n$ , $u_i \ne v_i$ ), describing a query "compute the length of the shortest path between vertices $u_i$ and $v_i$ ".
The second line contains $n$ integers $w_{1, 2}$ , $w_{3,4}$ , ..., $w_{2n - 1, 2n}$ ( $1 \le w_{i, i + 1} \le 10^{12}$ ). 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 $x_i$ , $y_i$ , $w_{i, 1}$ and $w_{i, 2}$ ( $1 \le x_i, y_i \le n$ , $x_i \ne y_i$ , $1 \le w_{i, j} \le 10^{12}$ ); it describes two edges: one connecting $2x_i - 1$ with $2y_i - 1$ and having weight $w_{i, 1}$ ; another connecting $2x_i$ with $2y_i$ and having weight $w_{i, 2}$ .
The next line contains one integer $q$ ( $1 \le q \le 6 \cdot 10^5$ ) — the number of queries.
Then $q$ lines follow, $i$ -th line contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le 2n$ , $u_i \ne v_i$ ), describing a query "compute the length of the shortest path between vertices $u_i$ and $v_i$ ".
输出格式
Print $q$ integers, $i$ -th integer should be equal to the answer to the $i$ -th query.
输入输出样例
输入 #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:


C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted