P10658 BZOJ2959 Long-Distance Run.
Background
A school has launched a popular “Sunshine Long-Distance Run” activity. To “work healthily for the motherland for fifty years”, students leave their dorms, classrooms, and labs to join a $3000$-meter run on the playground. In no time, the playground becomes packed with people, creating an unprecedented lively scene.
Description
To help students supervise themselves better, the school uses a card-swiping system. There are $n$ locations in the school, labeled by integers $1 \sim n$. Each location has several card-swiping machines.
There are three types of events:
1. Build a track connecting location $A$ and location $B$.
2. The number of card-swiping machines at location $A$ becomes $B$.
3. Perform a long-distance run. Ask: if a student starts from location $A$ and finally arrives at location $B$, what is the maximum number of times they can swipe their card. The specific rules are:
- When the student arrives at a location, they may swipe on every machine there. But each machine can be used at most once. That is, even if they visit the same location multiple times, they cannot swipe on the same machine more than once. For safety, each track must be assigned a direction, and it can only be traveled one-way along that direction. The maximum number of swipes is defined as: after choosing directions for all tracks arbitrarily, and choosing any path from $A$ to $B$, the maximum total number of swipes possible.
Input Format
The first line contains two positive integers $n, m$.
The second line contains $n$ positive integers. The $i$-th integer is the number of card-swiping machines at location $i$.
The next $m$ lines each contain three non-negative integers $P, A, B$, describing one event. $P$ is the event type, and $A, B$ are the two parameters of the event.
Output Format
For each event with $P = 3$, output one line containing one integer: the maximum number of swipes possible when traveling from $A$ to $B$ along some path, with track directions chosen arbitrarily. If $B$ cannot be reached, output $-1$.
Explanation/Hint
For all testdata, $1 \leq m \leq 5n$, $1 \leq n \leq 1.5 \times 10^5$.
It is guaranteed that initially there are no tracks between any locations.
In each line, adjacent numbers are separated by a single space.
All location indices are between $1$ and $n$. The number of card-swiping machines at each location never exceeds $10^4$. $P \in \{1, 2, 3\}$.
Translated by ChatGPT 5