luogu#P5236. 【模板】静态仙人掌
【模板】静态仙人掌
Background
This is a template problem for the Static Cactus (Block Forest Data Structure).
If you do not know what a cactus is, here is the definition of an undirected cactus graph:
An undirected connected graph in which any edge appears in at most one simple cycle is called a cactus.
Problem Description
You are given a cactus graph with vertices and edges, and queries.
Each query gives two vertices . Find the shortest path distance between them.
The input is guaranteed to have no multiple edges.
Input Format
The first line contains three positive integers , with the meanings as described above.
The next lines each contain three positive integers , indicating an undirected edge between and with weight .
Then follow lines, each containing two positive integers , asking for the shortest path from to .
Output Format
Output lines, each with one positive integer, the answer for the corresponding query.
9 10 2
1 2 1
1 4 1
3 4 1
2 3 1
3 7 1
7 8 2
7 9 2
1 5 3
1 6 4
5 6 1
1 9
5 7
5
6
9 10 3
1 2 1
2 3 1
2 4 4
3 4 2
4 5 1
5 6 1
6 7 2
7 8 2
8 9 4
5 9 2
1 9
5 8
3 4
7
5
2
Hint
Explanation for Sample 1:
The cactus in Sample 1 looks like this:

There are two queries: the shortest path for and for .
Obviously, the answers are and .
Constraints:
The input is guaranteed to have no multiple edges.
Note that the time limit is .
Translated by ChatGPT 5