103017: [Atcoder]ABC301 Ex - Difference of Distance

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $625$ points

Problem Statement

We have a connected undirected graph with $N$ vertices numbered $1$ to $N$ and $M$ edges numbered $1$ to $M$. Edge $i$ connects vertex $U_i$ and vertex $V_i$, and has an integer weight of $W_i$. For $1\leq s,t \leq N,\ s\neq t$, let us define $d(s,t)$ as follows.

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

Answer $Q$ queries. The $j$-th query is as follows.

  • You are given $A_j,S_j,T_j$. By what value will $d(S_j,T_j)$ increase when the weight of edge $A_j$ is increased by $1$?

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

Constraints

  • $2\leq N \leq 2\times 10^5$
  • $N-1\leq M \leq 2\times 10^5$
  • $1 \leq U_i,V_i \leq N$
  • $U_i \neq V_i$
  • $1 \leq W_i \leq M$
  • The given graph is connected.
  • $1\leq Q \leq 2\times 10^5$
  • $1 \leq A_j \leq M$
  • $1 \leq S_j,T_j \leq N$
  • $S_j\neq T_j$
  • All values in the input are integers.

Input

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

$N$ $M$
$U_1$ $V_1$ $W_1$
$\vdots$
$U_M$ $V_M$ $W_M$
$Q$
$A_1$ $S_1$ $T_1$
$\vdots$
$A_Q$ $S_Q$ $T_Q$

Output

Print $Q$ lines. The $j$-th line $(1\leq j \leq Q)$ should contain the answer to the $j$-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)$ in the given graph. The maximum weight of an edge along the path $4 \rightarrow 1 \rightarrow 2 \rightarrow 6$ is $5$, which is the minimum over all paths connecting vertex $4$ and vertex $6$, so $d(4,6)=5$.

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


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.

Input

题意翻译

给一张 $n$ 个点 $m$ 条边的带权无向图,$q$ 组询问,每次给 $(x,y,a)$ 问让第 $a$ 条边边权加一后 $(x,y)$ 的最小瓶颈路变没变。 $n,m,q\le 2\times 10^5$。

Output

得分:$625$分

问题描述

我们有一个用数字$1$到$N$标记的顶点数为$N$的连通无向图,用数字$1$到$M$标记的边数为$M$。第$i$条边连接顶点$U_i$和顶点$V_i$,权重为整数$W_i$。对于$1\leq s,t \leq N,\ s\neq t$,我们定义$d(s,t)$如下。

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

回答$Q$个查询。第$j$个查询如下。

  • 给你$A_j,S_j,T_j$。当边$A_j$的权重增加$1$时,$d(S_j,T_j)$会增加多少?

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

约束条件

  • $2\leq N \leq 2\times 10^5$
  • $N-1\leq M \leq 2\times 10^5$
  • $1 \leq U_i,V_i \leq N$
  • $U_i \neq V_i$
  • $1 \leq W_i \leq M$
  • 给定的图是连通的。
  • $1\leq Q \leq 2\times 10^5$
  • $1 \leq A_j \leq M$
  • $1 \leq S_j,T_j \leq N$
  • $S_j\neq T_j$
  • 输入中的所有值都是整数。

输入

输入通过标准输入给出,格式如下:

$N$ $M$
$U_1$ $V_1$ $W_1$
$\vdots$
$U_M$ $V_M$ $W_M$
$Q$
$A_1$ $S_1$ $T_1$
$\vdots$
$A_Q$ $S_Q$ $T_Q$

输出

打印$Q$行。 第$j$行 $(1\leq j \leq Q)$ 应包含第$j$个查询的答案。


样例输入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

样例输出1

0
0
0
0
0
1
1

image of the given graph

上图中黑色表示边的编号,蓝色表示边的权重。

下面我们解释第一个到第六个查询。

首先,考虑给定图中$d(4,6)$。 连接顶点$4$和顶点$6$的所有路径中,路径$4 \rightarrow 1 \rightarrow 2 \rightarrow 6$上的边的最大权重为$5$,这是连接顶点$4$和顶点$6$的所有路径中的最小值,因此$d(4,6)=5$。

接下来,考虑当边$x\ (1 \leq x \leq 6)$的权重增加$1$时,$d(4,6)$的增量。

当$x=6$时,我们有$d(4,6)=6$,增量为$1$。而当$x \neq 6$时,我们有$d(4,6)=5$,增量为$0$。 例如,当$x=3$时,路径$4 \rightarrow 1 \rightarrow 2 \rightarrow 6$上的边的最大权重为$6$,但路径$4 \rightarrow 3 \rightarrow 1 \rightarrow 2 \rightarrow 6$上的边的最大权重为$5$,因此$d(4,6)$仍然是$5$。


样例输入2

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

样例输出2

0

给定图中可能包含多边。

加入题单

上一题 下一题 算法标签: