题目大意:给出一个n个节点的图,求最大边权值减去最小边权值最小的生成树。
题解
Flash Hu大佬一如既往地强
先把边从小到大排序
然后依次加入每一条边
如果已经连通就把路径上权值最小的边删去
然后记得更新答案
ps:不是很明白为啥我洛谷上吸了氧还跑得更慢了233
1 //minamoto 2 #include3 #include 4 #include 5 #define inf 0x3f3f3f3f 6 using namespace std; 7 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) 8 char buf[1<<21],*p1=buf,*p2=buf; 9 template inline bool cmin(T&a,const T&b){ return a>b?a=b,1:0;}10 inline int read(){11 #define num ch-'0'12 char ch;bool flag=0;int res;13 while(!isdigit(ch=getc()))14 (ch=='-')&&(flag=true);15 for(res=num;isdigit(ch=getc());res=res*10+num);16 (flag)&&(res=-res);17 #undef num18 return res;19 }20 const int N=50005,M=200005,K=N+M;21 struct edge{22 int u,v,e;23 inline bool operator <(const edge &b)const24 { return e