A976.Wormhole Sort--Silver
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:128MB
题目描述
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≤N≤105),
conveniently numbered 1…N, are scattered throughout the barn at N
distinct locations, also numbered 1…N, such that cow i is at
location pi. But this morning there are also M wormholes (1≤M≤105), numbered 1…M, where wormhole i bidirectionally connects
location ai with location bi, and has a width wi (1≤ai,bi≤N,ai=bi,1≤wi≤109).
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≤i≤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.
输入格式
- Test cases 3-5 satisfy N,M≤1000.
- Test cases 6-10 satisfy no additional constraints.
输出格式
The first line contains two integers, N and M.
The second line contains the N integers p1,p2,…,pN. It is
guaranteed that p is a permutation of 1…N.
For each i between 1 and M, line i+2 contains the integers ai,
bi, and wi.
输入输出样例
输入#1
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
说明/提示
9