#P9678. [ICPC2022 Jinan R] Tree Distance

[ICPC2022 Jinan R] Tree Distance

题目描述

You are given an unrooted weighted tree TT with vertices 1,2,,n1, 2, \ldots, n. Please answer some queries.

We define dist(i,j)\texttt{dist}(i,j) as the distance between vertex ii and vertex jj in TT.

For each query, you are given two integers l,rl, r. Please answer the value of

minli<jr(dist(i,j)).\min_{l\le i< j\le r}(\texttt{dist}(i,j)).

输入格式

The first line contains one integer n (1n2×105)n~(1\leq n\le 2 \times 10^5), the number of vertices in the tree.

Each of the next n1n-1 lines describes an edge of the tree. Edge ii is denoted by three integers ai,bi,wia_i, b_i, w_i (1ai,bin,1wi109)(1\le a_i, b_i\le n, 1\le w_i\le 10^9), the labels of vertices it connects and its weight.

Then one line contains one integer q (1q106)q~(1\leq q\le 10^6), the number of queries.

Each of the following qq lines contains two integers l,r (1lrn)l, r~(1\le l \le r\le n) describing a query.

It is guaranteed that the given edges form a tree.

输出格式

For each query, output the answer in one line. If there is no i,ji,j such that li<jrl\le i<j\le r, the answer is 1-1.

题目大意

给你一棵 nn 个点的树。记 dist(i,j)\operatorname{dist}(i,j) 为树上 i,ji,j 之间唯一简单路径的长度。

你要回答 qq 次询问:给定 l,rl,r,求 $\min\limits_{l\leq i<j\leq r}(\operatorname{dist}(i,j))$。

5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5

-1
3
7
7
2