#C9063. Algoburg Tunnel Repair Optimization
Algoburg Tunnel Repair Optimization
Algoburg Tunnel Repair Optimization
In the underground city of Algoburg, a network of tunnels requires repair. The city has N junctions and M tunnels connecting them. Each tunnel is described by two endpoints and an integer repair cost w. Furthermore, a subset of these tunnels is reserved and must be included in the repair plan. Their indices are given as a list of Q 1-indexed values.
The final repair cost is computed by taking the bitwise AND (\(\wedge\)) of the repair costs of all the reserved tunnels and each additional tunnel that is selected to complete the connectivity. In other words, if the reserved tunnels have costs \(w_1, w_2, \ldots, w_q\) then the reserved cost is defined as:
[ R = (\cdots((1 \wedge w_1) \wedge w_2) \wedge \cdots \wedge w_q), ]
After performing a union of the reserved tunnels (using a union‐find structure), you must choose additional tunnels (from the non-reserved ones) one by one – each chosen tunnel must connect two previously disconnected components. For every chosen tunnel with cost \(w\), a candidate repair cost is calculated as:
[ C = R \wedge w. ]
Your task is to determine the minimal candidate repair cost that can be achieved and count the number of different ways to achieve that cost. Since the number of ways can be huge, output it modulo \(10^9+7\). It is guaranteed that the input data is such that at least one extra tunnel can be chosen to help connect the network.
inputFormat
The first line contains two integers \(N\) and \(M\): the number of junctions and tunnels.
The next \(M\) lines each contain three integers \(u\), \(v\), and \(w\), representing a tunnel connecting junctions \(u\) and \(v\) with repair cost \(w\>.
The next line contains an integer \(Q\), the number of reserved tunnel indices.
If \(Q > 0\), the following line contains \(Q\) space-separated integers representing the 1-indexed reserved tunnel indices.
outputFormat
Output two space-separated integers: the minimal possible total repair cost and the number of ways to achieve this cost modulo \(10^9+7\).
## sample4 5
1 2 4
2 3 5
3 4 6
1 4 7
2 4 3
2
2 3
0 1