#P15604. [ICPC 2021 Jakarta R] Bicycle Tour

[ICPC 2021 Jakarta R] Bicycle Tour

题目描述

There are NN junctions in Jakarta (numbered from 11 to NN) that are connected by MM bidirectional roads such that from any junction you can reach any other junctions by going through one or more roads. The ithi^{th} junction has a height of HiH_i.

Ahmad loves to cycle, especially on flat roads. He argues that you need more power to cycle if the road is uphill or downhill. Specifically, to cycle on a road connecting junction ii and junction jj, you will require a power of HiHj|H_i - H_j|. The required power to cycle a set of roads is equal to the highest required power to cycle each of those roads. For example, let there be 33 roads where each requires a power of 1010, 1212, and 77 to cycle, then the required power to cycle through all those roads is max(10,12,7)=12\max(10, 12, 7) = 12.

A cycling route from junction ii is defined as a tour that starts at junction ii, going to one or more other junctions while not using the same road more than once, and ends at the same junction ii.

Ahmad would like to find a cycling route from each junction that has a minimum required power, and that is your task in this problem. For each junction, you need to output the required power of a cycling route from that junction that has the minimum required power. There might be a case where a cycling route from a junction is not possible; in such a case, the output should be 1-1 for the respective junction.

For example, let N=8N = 8, H1..8={5,2,7,0,10,6,6,6}H_{1..8} = \{5, 2, 7, 0, 10, 6, 6, 6\}, M=11M = 11, and the roads be shown in the following figure. The label on each node indicates the junction number while the number on each edge indicates the required power to cycle through that road. Notice that the required power to cycle through each road can be obtained from the given H1..8H_{1..8}.

:::align{center} :::

The following are the cycling routes with the minimum required power from each junction:

  • Junction 11: $1 \rightarrow 2 \rightarrow 7 \rightarrow 6 \rightarrow 1$, with a required power of 44.
  • Junction 22: $2 \rightarrow 1 \rightarrow 6 \rightarrow 7 \rightarrow 2$, with a required power of 44.
  • Junction 33: 32133 \rightarrow 2 \rightarrow 1 \rightarrow 3, with a required power of 55.
  • There is no possible cycling route from junction 44.
  • Junction 55: $5 \rightarrow 3 \rightarrow 1 \rightarrow 2 \rightarrow 5$, with a required power of 88.
  • Junction 66: 67866 \rightarrow 7 \rightarrow 8 \rightarrow 6, with a required power of 00.
  • Junction 77: 78677 \rightarrow 8 \rightarrow 6 \rightarrow 7, with a required power of 00.
  • Junction 88: 86788 \rightarrow 6 \rightarrow 7 \rightarrow 8, with a required power of 00.

输入格式

Input begins with a line containing two integers NN MM (2N1000002 \leq N \leq 100\,000; N1M200000N-1 \leq M \leq 200\,000) representing the number of junctions and the number of bidirectional roads, respectively. The second line contains NN integers HiH_i (0Hi1090 \leq H_i \leq 10^9) representing the height of the ithi^{th} junction. The next MM lines, each contains two integers uiu_i viv_i (1ui<viN1 \leq u_i < v_i \leq N) representing a bidirectional road connecting junction uiu_i and viv_i. It is guaranteed that from any junction you can reach any other junctions by going through one or more roads. It is also guaranteed that for any pairs of junctions (u,v)(u, v), there is at most one bidirectional road connecting junction uu and junction vv.

输出格式

Output contains NN integers in a line each separated by a single space. The ithi^{th} integer represents the required power of a cycling route from junction ii with the minimum required power. If there is no possible cycling route from junction ii, then the ithi^{th} integer is 1-1.

8 11
5 2 7 0 10 6 6 6
1 2
1 3
2 3
2 4
2 5
2 7
3 5
1 6
6 7
6 8
7 8
4 4 5 -1 8 0 0 0
4 5
10 20 30 40
1 2
1 3
1 4
2 3
3 4
20 20 20 30

5 4
72 35 22 49 108
1 2
2 3
3 4
4 5
-1 -1 -1 -1 -1