Wormhole Sort//kruskal//排位3

Wormhole Sort//kruskal


题目

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.

Input
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.

Output
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.

Examples
inputCopy
4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3
outputCopy
9
inputCopy
4 1
1 2 3 4
4 2 13
outputCopy
-1
Note
The first example 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.

The second example is no wormholes are needed to sort the cows.
题意
给两点和边权,各个牛回位置,要求最大边权最小值

思路

典型kruskal

代码

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#define pi 3.1415926
using namespace std;
typedef long long ll;
const ll inf=100000000000;
int n,m,pos[100005],tot = 0,root[100005], ans = -1;
struct Edge{
    int from,to,dis;
}e[300005];
int findx(int x){
    if(root[x]==x) return x;
    else return root[x]=findx(root[x]);
}
void Union(int x, int y){
    root[findx(y)] = findx(x);
}
void add(int f,int t,int d){
    e[++tot].from = f;
    e[tot].to = t;
    e[tot].dis = d;
}
bool cmp(Edge a, Edge b){
    return a.dis>b.dis;
}
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++)
    cin>>pos[i];
    for (int i=0;i<m;i++){
	    int a,b,c;
	    cin>>a>>b>>c;
	    add(a,b,c);
	    }
	    sort(e+1,e+tot+1,cmp);
	    int cnt = 1;
	    for (int i=1;i<=n;i++)
	    root[i] = i;
	    for (int i=1;i<=tot;i++){
	    while(findx(cnt)==findx(pos[cnt])) {
	        cnt++;
	        if (cnt>n) goto A;
	    }
	    if (findx(pos[e[i].from])!=findx(pos[e[i].to])){
	        Union(pos[e[i].from],pos[e[i].to]);
	        ans = e[i].dis;
	    }
    }
    A:printf("%d",ans);
    return 0;
}

注意

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值