其实就是转换成求最小生成树中的最大支路,套模板
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define M 50009
using namespace std;
const int maxn=2e6+10,mod=100003;
#define pb push_back
int n,m,ans;
inline int read(){
char ch;
int f=1,res=0;
while((ch=getchar())<'0'||ch>'9')
if(ch=='-') f=-1;
while(ch>='0'&&ch<='9')
{
res=(res<<1)+(res<<3)+(ch^48);//注意位运算的优先级别
ch=getchar();
}
return f*res;
}
struct Edge{
int s,e,w;
Edge(int ss,int ee,int ww): s(ss),e(ee),w(ww){ }
Edge() {}
bool operator< (const Edge &e1) const{
return w<e1.w;
}
};
vector<Edge> edges;
vector <int>parent;
int GetRoot(int a)
{
if(parent[a]==a)
return a;
parent[a]=GetRoot(parent[a]);
return parent[a];
}
void Merge(int a,int b){
int p1=GetRoot(a);
int p2=GetRoot(b);
if(p1==p2) return ;
parent[p2]=p1;
}
int main(){
int i,j,k,root;
n=read();m=read();root=read();
for(i=0;i<=n;i++) parent.push_back(i);
for(k=0;k<m;k++)
{
int w;
i=read();j=read();
w=read();
edges.push_back(Edge(i,j,w));
edges.push_back(Edge(j,i,w));
}
sort(edges.begin(),edges.end());
int done=0;int totallen=0;
for(i=0;i<edges.size();i++)
{
if(GetRoot(edges[i].s)!=GetRoot(edges[i].e)){
Merge(edges[i].e,edges[i].s);
++done;if(edges[i].w>totallen) totallen=edges[i].w;
}
if(done==n-1) break;
}
printf("%d\n",totallen);
return 0;
}