我也不知道为什么我要来写这个…怕是写水题写上瘾了(bu
#include<cstdio> #include<algorithm> #define rep(i,n) for(register int i=1;i<=n;i++) const int N=10005; struct edge { int u,v,c; }e[N]; int n,m,mx,s[N],pre[N]; inline int find(int x){return pre[x]==x?x:find(pre[x]);} inline int max(int a,int b){return a>b?a:b;} inline void unite(int x,int y) { int t=s[x]+s[y]; if(s[x]>s[y])pre[y]=x,s[x]=s[y]=t; else pre[x]=y,s[x]=s[y]=t; } inline bool cmp(const edge &x,const edge &y) { return x.c<y.c; } int main() { scanf("%d%d",&n,&m); rep(i,n)pre[i]=i,s[i]=1; rep(i,m)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c); std::sort(e+1,e+m+1,cmp); rep(i,m) { int u,v,c;u=e[i].u;v=e[i].v;c=e[i].c; int fu=find(u),fv=find(v); if(fu==fv)continue; unite(fu,fv);mx=max(mx,c); } printf("%d %d",n-1,mx); }
原文地址:https://www.cnblogs.com/yoooshinow/p/8358968.html
时间: 2024-10-11 10:17:56