#include<bits/stdc++.h>
using namespace std;
int n,m;
int f[20000007];
int ans;
int cnt;
int i,j,k;
struct ROAD
{
int u;
int v;
int w;
}r[20000007];
bool cmp(ROAD a,ROAD b)
{
return a.w<=b.w;
}
int find(int x)
{
return ((f[x]==x)?x:find(f[x]));
}
int main(int argc,char** argv)
{
cin >>n>>m;
for (i=1;i<=n;i++)
f[i]=i;
for (i=0;i<m;i++)
cin >>r[i].u>>r[i].v>>r[i].w;
sort (r,r+m,cmp);
for (i=0;i<m;i++)
{
if (find(r[i].u)==find(r[i].v))
continue;
f[find(r[i].v)]=find(r[i].u);
cnt++;
if (cnt==n-1)
{
ans=r[i].w;
break;
}
}
cout <<ans<<endl;
return 0;
}
萌新求助,我数组开的足够大,可是为什么会RE呢?(百思不得其解)