Logo
(追記) (追記ここまで)

18321번 - Wormhole Sort 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB70527523741.003%

문제

Farmer John's cows have grown tired of his daily request that they sort themselves before leaving the barn each morning. They have just completed their PhDs in quantum physics, and are ready to speed things up a bit.

This morning, as usual, Farmer John's $N$ cows (1ドル \leq N \leq 10^5$), conveniently numbered 1ドル \dots N,ドル are scattered throughout the barn at $N$ distinct locations, also numbered 1ドル \dots N,ドル such that cow $i$ is at location $p_i$. But this morning there are also $M$ wormholes (1ドル \leq M \leq 10^5$), numbered 1ドル \dots M,ドル where wormhole $i$ bidirectionally connects location $a_i$ with location $b_i,ドル and has a width $w_i$ (1ドル\le a_i,b_i\le N, a_i\neq b_i, 1\le w_i\le 10^9$).

At any point in time, two cows located at opposite ends of a wormhole may choose to simultaneously swap places through the wormhole. The cows must perform such swaps until cow $i$ is at location $i$ for 1ドル \leq i \leq N$.

The cows are not eager to get squished by the wormholes. Help them maximize the width of the least wide wormhole which they must use to sort themselves. It is guaranteed that it is possible for the cows to sort themselves.

입력

The first line contains two integers, $N$ and $M$.

The second line contains the $N$ integers $p_1, p_2, \dots, p_N$. It is guaranteed that $p$ is a permutation of 1ドル\ldots N.$

For each $i$ between 1ドル$ and $M,ドル line $i+2$ contains the integers $a_i,ドル $b_i,ドル and $w_i$.

출력

A single integer: the maximum minimal wormhole width which a cow must squish itself into during the sorting process. If the cows do not need any wormholes to sort themselves, output $-1$.

제한

예제 입력 1

4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3

예제 출력 1

9

Here is one possible way to sort the cows using only wormholes of width at least 9:

  • Cow 1 and cow 2 swap positions using the third wormhole.
  • Cow 1 and cow 3 swap positions using the first wormhole.
  • Cow 2 and cow 3 swap positions using the third wormhole.

예제 입력 2

4 1
1 2 3 4
4 2 13

예제 출력 2

-1

No wormholes are needed to sort the cows.

힌트

출처

Olympiad > USA Computing Olympiad > 2019-2020 Season > USACO 2020 January Contest > Silver 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /