Love Letter

시간 제한2.5초메모리 제한1024 MB

요약
나이가 모두 다른 용들이 있고, 나이 차이만큼 시간이 걸려 편지를 보내되 친구 사이는 0의 시간이 걸린다. 용 1에서 모든 용까지의 최단 시간을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 정렬, 분할 정복
정답자
아직 제출이 없습니다

문제

There are nn dragons numbered from 11 to nn. Dragon ii has age a_ia\_i. The values of a_ia\_i are distinct. Dragon 11 is Evirir the dragon.

Evirir has a letter it wants to send to dragon tt. To avoid awkwardness caused by age difference, Evirir can send the letter to another dragon (not necessarily dragon tt). The dragon who received the letter can then send the letter to another dragon, and so on. The goal is to eventually send the letter to dragon tt.

For all ii (1≤i≤n1 \le i \le n), when dragon ii has the letter, it can send the letter to dragon jj in ∣a_i−a_j∣|a\_i - a\_j| time1^1. A dragon can "send" the letter to itself in 00 time. However, there are kk pairs of dragons who are close friends. If dragons ii and jj are close friends, then it takes 00 time instead for dragon ii to send a letter to dragon jj (and vice versa).

For each dragon tt (1≤t≤n1 \le t \le n), answer the question (independently): What is the minimum total time needed for dragon 11 to send a letter to dragon tt?

1^1Note: ∣x∣|x| denotes the absolute value of xx. For example, ∣9∣=9|9| = 9, ∣−6∣=6\lvert -6 \rvert = 6, and ∣0∣=0|0| = 0. See \url{https://en.wikipedia.org/wiki/Absolute\_value} for more information.

입력

The first line contains two space-separated integers nn and kk (1≤n≤2⋅1051 \le n\le 2 \cdot 10^5, 0≤k≤2⋅1050 \le k \le 2 \cdot 10^5) -- the number of dragons and the number of pairs of close friends.

The second line contains nn distinct space-separated integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1091 \le a\_i \le 10^9) -- the dragons' ages.

Then, kk lines follow. Each of the kk lines contains two space-separated integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v), which means that dragons uu and vv are close friends. It is guaranteed that the same pair of close friends will not appear twice (if (u,v)(u,v) appears, then (u,v)(u,v) and (v,u)(v,u) will not appear afterwards).

출력

Output nn space-separated integers d_1,d_2,…,d_nd\_1, d\_2, \ldots, d\_n, where d_id\_i is the minimum total time needed for dragon 11 to send the letter to dragon ii.

힌트

Explanation for the first sample:

When t=1t = 1, it takes 00 time because dragon 11 already has the letter.

When t=3t = 3, since dragon 11 and 33 are close friends, dragon 11 can send the letter directly to dragon 33 in 00 time.

When t=2t = 2, dragon 11 can send the letter to dragon 33 first in 00 time (they are close friends), then dragon 33 sends the letter to dragon 22 in ∣23−30∣=7|23 - 30| = 7 time. The total time taken is 0+7=70 + 7 = 7.

When t=8t = 8, dragon 11 can just send the letter directly to dragon 88, taking ∣50−47∣=3|50 - 47| = 3 time.

Here is one optimal way each for the remaining tt's (i→xji \xrightarrow{x} j means dragon ii sends the letter to dragon jj using xx time):

  • Dragon 44: 1→03→72→041 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4
  • Dragon 55: 1→03→72→04→751 \xrightarrow{0} 3 \xrightarrow{7} 2 \xrightarrow{0} 4 \xrightarrow{7} 5
  • Dragon 66: 1→03→07→261 \xrightarrow{0} 3 \xrightarrow{0} 7 \xrightarrow{2} 6
  • Dragon 77: 1→071 \xrightarrow{0} 7

예제2

  1. 예제 1

    입력
    8 4
    50 30 23 10 3 67 69 47
    3 7
    3 1
    2 4
    7 1
    
    예상 출력
    0 7 0 7 14 2 0 3
    
  2. 예제 2

    입력
    3 0
    2 3 1
    
    예상 출력
    0 1 1