CF1000G.Two-Paths

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with nn vertices. The edge {uj,vj}\{u_j, v_j\} has weight wjw_j . Also each vertex ii has its own value aia_i assigned to it.

Let's call a path starting in vertex uu and ending in vertex vv , where each edge can appear no more than twice (regardless of direction), a 2-path. Vertices can appear in the 2-path multiple times (even start and end vertices).

For some 2-path pp profit Pr(p)=vdistinct vertices in pavedistinct edges in pkewe\text{Pr}(p) = \sum\limits_{v \in \text{distinct vertices in } p}{a_v} - \sum\limits_{e \in \text{distinct edges in } p}{k_e \cdot w_e} , where kek_e is the number of times edge ee appears in pp . That is, vertices are counted once, but edges are counted the number of times they appear in pp .

You are about to answer mm queries. Each query is a pair of vertices (qu,qv)(qu, qv) . For each query find 2-path pp from ququ to qvqv with maximal profit Pr(p)\text{Pr}(p) .

输入格式

The first line contains two integers nn and qq ( 2n31052 \le n \le 3 \cdot 10^5 , 1q41051 \le q \le 4 \cdot 10^5 ) — the number of vertices in the tree and the number of queries.

The second line contains nn space-separated integers a1,a2,,ana_1, a_2, \dots, a_n (1ai109)(1 \le a_i \le 10^9) — the values of the vertices.

Next n1n - 1 lines contain descriptions of edges: each line contains three space separated integers uiu_i , viv_i and wiw_i ( 1ui,vin1 \le u_i, v_i \le n , uiviu_i \neq v_i , 1wi1091 \le w_i \le 10^9 ) — there is edge {ui,vi}\{u_i, v_i\} with weight wiw_i in the tree.

Next qq lines contain queries (one per line). Each query contains two integers quiqu_i and qviqv_i (1qui,qvin)(1 \le qu_i, qv_i \le n) — endpoints of the 2-path you need to find.

输出格式

For each query print one integer per line — maximal profit Pr(p)\text{Pr}(p) of the some 2-path pp with the corresponding endpoints.

输入输出样例

  • 输入#1

    7 6
    6 5 5 3 2 1 2
    1 2 2
    2 3 2
    2 4 1
    4 5 1
    6 4 2
    7 3 25
    1 1
    4 4
    5 6
    6 4
    3 4
    3 7
    

    输出#1

    9
    9
    9
    8
    12
    -14
    

说明/提示

Explanation of queries:

  1. (1,1)(1, 1) — one of the optimal 2-paths is the following: 1245423211 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1 . Pr(p)=(a1+a2+a3+a4+a5)(2w(1,2)+2w(2,3)+2w(2,4)+2w(4,5))=21212=9\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4 + a_5) - (2 \cdot w(1,2) + 2 \cdot w(2,3) + 2 \cdot w(2,4) + 2 \cdot w(4,5)) = 21 - 2 \cdot 12 = 9 .
  2. (4,4)(4, 4) : 42123244 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 4 . Pr(p)=(a1+a2+a3+a4)2(w(1,2)+w(2,3)+w(2,4))=19210=9\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4) - 2 \cdot (w(1,2) + w(2,3) + w(2,4)) = 19 - 2 \cdot 10 = 9 .
  3. (5,6)(5, 6) : 5423212465 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4 \rightarrow 6 .
  4. (6,4)(6, 4) : 642123246 \rightarrow 4 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 4 .
  5. (3,4)(3, 4) : 321243 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4 .
  6. (3,7)(3, 7) : 32124542373 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 7 .
首页