最最基础题
百练OJ有毒 POJ是对的,在百练怎么都不对
题目地址
#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=15000+5;
int p[1000+5],r[maxn];
struct edge{int from,to,weight;edge(int f=0,int t=0,int w=0):from(f),to(t),weight(w){}
}e[maxn];
bool cmp(int i,int j){return e[i].weight<e[j].weight;
}
int getParent(int x){if(p[x]==x) return x;else return p[x]=getParent(p[x]);
}
int main()
{int N,M,x,y,w;cin>>N>>M;for(int i=1;i<=N;i++) p[i]=i;for(int i=0;i<M;i++) r[i]=i;for(int i=0;i<M;i++){cin>>x>>y>>w;e[i]=edge(x,y,w);} sort(r,r+M,cmp);vector<int> ans;for(int i=0;i<M;i++){int idx=r[i];int x=getParent(e[idx].from),y=getParent(e[idx].to);if(x!=y) {ans.push_back(idx);p[x]=y;}if(ans.size()==N-1) break;}cout<<e[ans[N-2]].weight<<endl<<N-1<<endl;for(int i=0;i<N-1;i++){cout<<e[ans[i]].from<<' '<<e[ans[i]].to<<endl;}return 0;
}