Title Link: http://poj.org/problem?id=1861
Very similar to the previous topic, more interesting is a bit deceptive here, but I did not notice, Fluke. Fortunately, the sample did not look closely. The topic is only required to find the longest edge in the case of ensuring that all vertices are linked. The total weight of the path is not required to be minimal. So you can add any additional edges that are less than the longest edge weight, although he may be a ring. All have a wonderful sample. In fact, the idea of constructing the minimum spanning tree is completely.
#include <iostream> #include <algorithm> #include <string.h> #include <stack> #include < queue>using namespace Std;const int m=10046;int f[m];int r[m];int n,m;int res,num;struct edge{int u,v,w;} per[15005 ];bool CMP (Edge A, Edge b) {return A.W<B.W;} void Init () {for (int i=1; i<=n; i++) {r[i]=1; F[i]=i; }}//int find (int x)//{//if (x!=f[x])//Return F[x]=find (F[x]);//}int find (int x) {int r=x; while (R!=f[r]) {r=f[r]; } int k=x; while (k!=r) {int t=f[k]; F[k]=r; k=t; } return R;} void Union_set (int x,int y) {int tx=find (x); int Ty=find (y); if (tx==ty) return; else if (R[tx]>r[ty]) f[ty]=tx; else if (R[tx]<r[ty]) f[tx]=ty; else {f[tx]=ty; r[ty]++; }}void Kru () {//stack<int>cnt; queue<int>cnt; Res=-1; for (int i=0; i<m; i++) {if (Find (per[i].u)!=find (PER[I].V)) { num++; if (RES<PER[I].W) RES=PER[I].W; Union_set (PER[I].U,PER[I].V); Cnt.push (i); } if (num==n-1) {cout<<res<<endl<<cnt.size () <<endl; while (!cnt.empty ()) {int T=cnt.front (); Cnt.pop (); cout<<per[t].u<< "" <<per[t].v<<endl; } break; }}}int Main () {while (cin>>n>>m) {num=0; Init (); for (int i=0; i<m; i++) {cin>>per[i].u>>per[i].v>>per[i].w; } sort (per,per+m,cmp); Kru (); } return 0;}
Poj 1861 Network