RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0404 파일 입출력

Wormhole Sort

Gold II 골드 II
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

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.

Problem credits: Dhruv Rohatgi

제약

SCORING

  • Test cases 3-5 satisfy \(N,M\le 1000.\)
  • Test cases 6-10 satisfy no additional constraints.

Problem credits: Dhruv Rohatgi

입력 형식

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\).

파일 입력 / 출력
이 문제는 표준 입출력을 사용하지 않습니다. 프로그램은 다음 파일을 읽고 써야 합니다:
입력을 읽을 파일 wormsort.in
출력을 쓸 파일 wormsort.out
예제 1
입력
4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3
출력
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
출력
-1
설명

No wormholes are needed to sort the cows.

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2019-2020 > January > Silver

태그

평가 및 의견

Wormhole Sort

개요
출제자 난이도 Gold II 골드 II 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Wormhole Sort

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8