#abc301h. Ex - Difference of Distance

Ex - Difference of Distance

Score : 625625 points

问题描述

我们有一个包含 NN 个顶点的连通无向图,顶点编号从 11NN,并有 MM 条边,编号从 11MM。第 ii 条边连接顶点 UiU_i 和顶点 ViV_i,具有整数权重 WiW_i。对于 1s,tN1\leq s,t \leq Nsts\neq t,让我们定义 d(s,t)d(s,t) 如下:

  • 对于每条连接顶点 ss 和顶点 tt 的路径,考虑该路径上最大边权重。d(s,t)d(s,t) 是这个权重的最小值。

解答 QQ 个查询。第 jj 个查询如下:

  • 给定 Aj,Sj,TjA_j,S_j,T_j,当边 AjA_j 的权重增加 11 时,d(Sj,Tj)d(S_j,T_j) 将增加多少?

注意:每个查询实际上并不会改变边的权重。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a connected undirected graph with NN vertices numbered 11 to NN and MM edges numbered 11 to MM. Edge ii connects vertex UiU_i and vertex ViV_i, and has an integer weight of WiW_i. For 1s,tN, st1\leq s,t \leq N,\ s\neq t, let us define d(s,t)d(s,t) as follows.

  • For every path connecting vertex ss and vertex tt, consider the maximum weight of an edge along that path. d(s,t)d(s,t) is the minimum value of this weight.

Answer QQ queries. The jj-th query is as follows.

  • You are given Aj,Sj,TjA_j,S_j,T_j. By what value will d(Sj,Tj)d(S_j,T_j) increase when the weight of edge AjA_j is increased by 11?

Note that each query does not actually change the weight of the edge.

Constraints

  • 2N2×1052\leq N \leq 2\times 10^5
  • N1M2×105N-1\leq M \leq 2\times 10^5
  • 1Ui,ViN1 \leq U_i,V_i \leq N
  • UiViU_i \neq V_i
  • 1WiM1 \leq W_i \leq M
  • The given graph is connected.
  • 1Q2×1051\leq Q \leq 2\times 10^5
  • 1AjM1 \leq A_j \leq M
  • 1Sj,TjN1 \leq S_j,T_j \leq N
  • SjTjS_j\neq T_j
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

U1U_1 V1V_1 W1W_1

\vdots

UMU_M VMV_M WMW_M

QQ

A1A_1 S1S_1 T1T_1

\vdots

AQA_Q SQS_Q TQT_Q

Output

Print QQ lines. The jj-th line (1jQ)(1\leq j \leq Q) should contain the answer to the jj-th query.

Sample Input 1

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

Sample Output 1

0
0
0
0
0
1
1

image of the given graph

The above figure shows the edge numbers in black and the edge weights in blue.

Let us explain the first through sixth queries.

First, consider d(4,6)d(4,6) in the given graph. The maximum weight of an edge along the path 41264 \rightarrow 1 \rightarrow 2 \rightarrow 6 is 55, which is the minimum over all paths connecting vertex 44 and vertex 66, so d(4,6)=5d(4,6)=5.

Next, consider the increment of d(4,6)d(4,6) when the weight of edge x (1x6)x\ (1 \leq x \leq 6) increases by 11. When x=6x=6, we have d(4,6)=6d(4,6)=6, and the increment is 11. On the other hand, when x6x \neq 6, we have d(4,6)=5d(4,6)=5, and the increment is 00. For instance, when x=3x=3, the maximum weight of an edge along the path 41264 \rightarrow 1 \rightarrow 2 \rightarrow 6 is 66, but the maximum weight of an edge along the path $4 \rightarrow 3 \rightarrow 1 \rightarrow 2 \rightarrow 6$ is 55, so d(4,6)d(4,6) is still 55.

Sample Input 2

2 2
1 2 1
1 2 1
1
1 1 2

Sample Output 2

0

The given graph may contain multi-edges.

update @ 2024/3/10 08:28:28