HDU 6039 Gear Up 并查集 dfs序 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=6039

Gear Up

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description

constroy has some gears, each with a radius. Two gears are considered adjacent if they meet one of the following conditions:
1. They share a common edge (i.e. they have equal linear velocity).
2. They share a common shaft (i.e. they have equal angular velocity).
It is guaranteed that no pair of gears meets both of the above conditions.
A series of continuous adjacent gears constitutes a gear path. There is at most one gear path between each two gears.
Now constroy assigns an angular velocity to one of these gears and then asks you to determine the largest angular velocity among them.
sd0061 thinks this problem is too easy, so he replaces some gears and then asks you the question again.

Input

There are multiple test cases (about $30$).
For each test case:
The first line contains three integers $n, m, q$, the number of gears, the number of adjacent pairs and the number of operations. $(0 \leq m < n \leq 10^5, 0 \leq q \leq 10^5)$
The second line contains $n$ integers, of which the $i$-th integer represents $r_i$, the radius of the $i$-th gear. $(r_i \in \{2^\lambda \mid 0 \leq \lambda \leq 30\})$
Each of the next $m$ lines contains three integers $a, x, y$, the $x$-th gear and the $y$-th gear are adjacent in the $a$-th condition. $(a \in \{1, 2\}, 1 \leq x, y \leq n, x \neq y)$
Each of the next $q$ line contains three integers $a, x, y$, an operation ruled in the following: $(a \in \{1, 2\}, 1 \leq x \leq n, y \in \{2^\lambda \mid 0 \leq \lambda \leq 30\})$
$a = 1$ means to replace the $x$-th gear with another one of radius $y$.
$a = 2$ means to assign angular velocity $y$ to the $x$-th gear and then determine the maximum angular velocity.

Output

For each test case, firstly output "Case #$x$:" in one line (without quotes), where $x$ indicates the case number starting from $1$, and then for each operation of $a = 2$, output in one line a real number, the natural logarithm of the maximum angular velocity, with the precision of $3$ digits.

Sample Input

Sample Output

Source

题意

给n个齿轮,每个齿轮都有各自的半径,再给m对齿轮关系,有共边和共轴两种。有q次操作,每次操作会修改某个齿轮的半径,或者是给以某个齿轮一个角速度,问和他连通的齿轮组内角速度最大齿轮的角速度。保证输入的半径或者角速度都是2的整数次方幂。输出的答案取自然对数。保证齿轮关系构成的图没有环。

思路

使用并查集维护共轴的齿轮关系。在每个齿轮连通块中选取一个参考齿轮,将所有的速度和半径对2取对数后,角速度之间差距就从倍数变成了差值,方便计算。从参考齿轮开始,进行dfs,并维护dfs序,优先dfs和这个齿轮共边的齿轮,并计算每个齿轮对于参考齿轮的角速度差,和这个齿轮u共边的齿轮v,他们的角速度都是都是比u的相对角速度快r[u]-r[v],其中r为半径,并且记每个v齿轮在树上的父亲为u。记录下这个dfs序的起止位置为edgeL[u],edgeR[u]。再dfs和这个齿轮共轴的齿轮,这些齿轮的角速度和u相同,并且记这些齿轮的在树上的父亲为不存在。记录这段dfs序列的起止位置为shaftL[u]和shaftR[u]。

询问的时候,先计算出参考齿轮的角速度,再求整个连通块内的最大值即可,区间应该为edgeL[u]~shaftR[u]。

修改分为两种情况。如果这个齿轮在树上没有父亲,那么修改他的半径,只会影响和他共边的齿轮以及共边齿轮连接的齿轮,区间为edgeL[u]+1~edgeR[u]。如果有父亲,则会影响他自己的角速度以及和他共轴齿轮及他们连接的齿轮,区间为edgeL[u]~edgeL[u],shaftL[u]~shaftR[u]。每次修改,对上述区间进行区间累加,维护最大值即可。