代码:
#include<queue>
#include<cstdio>
using namespace std;
const int MAXN = 500005;
int n,m,root;
struct Edge{
int start,end,cost;
Edge(int s,int e,int c):start(s),end(e),cost(c){};
bool operator < (const Edge& e) const
{
return cost>e.cost;
}
};
priority_queue<Edge> pq;
int father[MAXN];
int ans=0;
int findfather(int x)
{
if(x==father[x])
return x;
int temp=findfather(father[x]);
father[x]=temp;
return temp;
}
int main()
{
scanf("%d\n%d\n%d",&n,&m,&root);
getchar();
int a,b,c;
while(m--)
{
scanf("%d %d %d",&a,&b,&c);
pq.push(Edge(a,b,c));
}
for(int i=1;i<=n;i++)
{
father[i]=i;
}
while(!pq.empty())
{
Edge edge=pq.top();
pq.pop();
int fs=findfather(edge.start);
int fe=findfather(edge.end);
if(fs!=fe)
{
father[fs]=fe;
ans=edge.cost;
}
}
printf("%d",ans);
return 0;
}