水题 最小生成树求最大边 表示并查集+kruskal真的好用
- Source Code
#include <iostream> #include "stdio.h" #include "stdlib.h" #include "string.h" #include "math.h" #include <algorithm> using namespace std; #define inf 99999999 int a[10002],b[10002],c[10002],pos[10002],father[10002]; int find1(int x) { if(x==father[x]) return x; else return father[x]=find1(father[x]); } int cmp(int a,int b) { return c[a]<c[b]; } int main() { int n,m,x,y,ans=0,maxn=0; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d%d",&a[i],&b[i],&c[i]); pos[i]=i; father[i]=i; } sort(pos,pos+m,cmp); for(int i=0;i<m;i++) { x=find1(a[pos[i]]);y=find1(b[pos[i]]); if(x!=y) { father[x]=y; maxn=max(c[pos[i]],maxn); } } printf("%d\n",maxn); }