1977: [BeiJing2010 team] sub-niche tree time limit: Sec Memory Limit: MB
Submit: 2313 Solved: 544
[Submit] [Status] [Discuss] Description
Small C recently learned a lot of the minimum spanning tree algorithm, Prim algorithm, Kurskal algorithm, elimination loop algorithm and so on. Just as small c complacent, small P again to pour small c cold water. Small P said, let small C to find a non-map of the sub-niche into a tree, and this sub-niche into a tree still have to be strict small, that is to say: if the minimum spanning tree selection of the edge set is EM, the strict sub-niche into a tree selected edge set is ES, then need to meet: (Value (e) indicates the weight of the edge e) This little C-blindfolded, he looked for To you, I hope you help him solve the problem.
Input
The first line contains two integers, N and m, representing the number of points and sides of the graph without a direction. Next m lines, 3 x y Z for each line, have an edge between point x and Point y, and the weight of the Edge is Z.
Output
Contains a row, only a number, indicating the strict sub-niche into the tree Benquan and. (Data assurance must exist strictly sub-niche into a tree)
Sample Input5 6
1 2 1
1 3 2
2 4 3
3 5 4
3 4 3
4 5 6Sample Output
There is no self-loop in the data, 50% of data N≤2 M≤3 000; 80% of data N≤50 m≤100 000; 100% of data N≤100 m≤300 000, margin value is non-negative and does not exceed 10^9.
Double.
The minimum spanning tree is obtained, and each non-tree edge is enumerated to replace the maximum edge optimal on the off-ring.
Then the equivalent of two points to the LCA of the maximum edge right, directly multiplied to do.
But attention is strictly the minimum spanning tree, then this non-tree edge and the maximum edge of the ring may be replaced by the ring on the second large side, then multiply the time to maintain a strict second-largest edge.
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < Cstring> #define LL Long long#define pa pair<int,int> #define INF 1000000005#define M 300000+5#define N 100000+5#i Nclude <queue>using namespace std;queue<int> q; LL pre=0;int a[5],fa[n],v[m],n,m,tot,h[n],d[n],f[n][21];struct edge{int x,y,v;} E[m];struct edge{int y,ne,v;} E[m*2];struct Ma{int A, B;} G[n][21],p;bool CMP (Edge A,edge b) {return A.V<B.V;} int getfather (int x) {return x==fa[x]?x:fa[x]=getfather (fa[x]);} void Kruscal () {sort (e+1,e+1+m,cmp), int cnt=1;for (int i=1;i<=m;i++) {int Fx=getfather (e[i].x), Fy=getfather (e[i].y ); if (Fx==fy) continue;cnt++;v[i]=1;fa[fx]=fy;pre+=e[i].v;}} void Addedge (int x,int y,int v) {E[++tot].y=y;e[tot].ne=h[x];e[tot].v=v;h[x]=tot;e[++tot].y=x;e[tot].ne=h[y];e[tot] . V=v;h[y]=tot;} void Build () {for (int i=1;i<=m;i++) if (V[i]) Addedge (E[I].X,E[I].Y,E[I].V); g[1][0].a=g[1][0].b=-inf;f[1][0]=0;d[ 1]=1;q.push (1); while (!q.empty ()) {int x=Q.front (); Q.pop (); for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (y==f[x][0]) continue;f[y][0]=x;d[y]=d[x]+1;g[y][0]. A=e[i].v;q.push (y);}}} void Update (ma &x,ma y) {/*a[0]=x.a,a[1]=x.b,a[2]=y.a,a[3]=y.b;sort (a,a+4); X.a=max (x.a,y.a); for (int k=2;k>=0 ; k--) if (a[k]!=a[k+1]) {x.b=a[k];break;} */if (X.A>Y.A) X.b=max (y.a,x.b), else if (x.a<y.a) X.b=max (x.a,y.b), Else X.b=max (x.b,y.b); X.a=max (X.A,Y.A);} void ST () {for (int j=1, (1<<j) <=n;j++) for (int i=1;i<=n;i++) {f[i][j]=f[f[i][j-1]][j-1]; Update (G[i][j],g[i][j-1]); Update (G[i][j],g[f[i][j-1]][j-1]);}} void Move (int &x,int deep) {for (int i=20;i>=0;i--) if (d[f[x][i]]>=deep) Update (P,g[x][i]), x=f[x][i];} void Getlca (int x,int y) {p.a=p.b=-inf;if (D[x]>d[y]) swap (x, y); Move (Y,d[x]), if (x==y) return;for (int i=20;i>=0;i--) if (F[x][i]!=f[y][i]) {update (p,g[x][i]), update (P,g[y][i]), X =f[x][i],y=f[y][i];} Update (p,g[x][0]), update (p,g[y][0]);} int main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) scanf ("%d%d%d",&E[I].X,&E[I].Y,&E[I].V); for (int i=1;i<=n;i++) fa[i]=i; Kruscal (); Build (); ST (); ll ans= (LL) 1e15;for (int i=1;i<=m;i++) if (!v[i]) {GETLCA (E[I].X,E[I].Y); if (P.A==E[I].V) ans=min (Ans,pre+e[i]. V-P.B); else Ans=min (ANS,PRE+E[I].V-P.A);} printf ("%lld\n", ans); return 0;}
"Bzoj 1977" [BeiJing2010 team] sub-niche into a tree