The sample of zoj is incorrect. The problem is to find the Minimum Spanning Tree and the longest path.
#include <vector>#include <list>#include <map>#include <set>#include <string.h>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;#define LL long long#define pi acos(-1)#define N 15010#define INF 999999999#define eps 1e-8//****************************************//zoj 1542 Network//Copyright@leolin. All rights reserved.//****************************************struct node{ int x,y,len;}e[N],hub[N];int fa[1100];int n,m;bool cmp(node a,node b){ return a.len<b.len;}int find(int x){ while(x!=fa[x]) x=fa[x]; return x;}void krasual(){ int i,j,k; for(i=0;i<=n;i++) fa[i]=i; sort(e+1,e+m+1,cmp); int max=0; int cnt=0; for(i=1;i<=m;i++) { int a=find(e[i].x); int b=find(e[i].y); if(a!=b) { fa[a]=b; if(max<e[i].len)max=e[i].len; hub[cnt++]=e[i]; } } printf("%d\n%d\n",max,cnt); for(i=0;i<cnt;i++) printf("%d %d\n",hub[i].x,hub[i].y);}int main(){//freopen("a.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].len); krasual(); } return 0;}