Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network,
each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
The first line of the input file contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 10^6. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs. Process to the end of file.
Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
4 6
1 Source: Northeastern Europe 2001, Northern Subregion
Copyright @ #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<iomanip> #include<vector> using namespace std; const int mm=1010; const int nn=15009; int root[mm],ran[mm]; struct node{int u,v,c;}edge[nn]; int look(int x) { if(x^root[x])root[x]=look(root[x]); return root[x]; } void uni(int x,int y) { if(ran[x]>ran[y])root[look(y)]=look(x),ran[x]+=ran[y]; else root[look(x)]=look(y),ran[y]+=ran[x]; } bool cmp(node a,node b) { return a.c<b.c; } int n,m; int main() { while(cin>>n>>m) { for(int i=0;i<m;i++) cin>>edge[i].u>>edge[i].v>>edge[i].c; sort(edge,edge+m,cmp); int len,num=0,top=-1; for(int i=0;i<=n;i++)root[i]=i,ran[i]=1; for(int i=0;i<m;i++) if(look(edge[i].u)^look(edge[i].v))///top跳跃指针存路径 uni(edge[i].u,edge[i].v),len=edge[i].c,++num,edge[i].c=top,top=i; cout<<len<<"\n"<<num<<"\n"; while(top!=-1) { cout<<edge[top].u<<" "<<edge[top].v<<"\n";top=edge[top].c; } } } |