P4180 "Template" strictly sub-niche into a tree [BJWC2010]

Source: Internet
Author: User

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 6

Sample Outputone Hint

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.

Ask yourself, What is the essence of Kruskal?

Greedy

The Kruskal algorithm has been shown for any u,v

Benquan with edge weights between U and v less than equals U to V not selected

So, not strictly the sub-niche into a tree as long as

Iterate through each of the selected edges (u,v,d), replacing the largest edge between U and V

Now, our mission is to keep the non-strict

Why is it not strict?

Because

Benquan of edge weights between U and v less than equals U to V not selected

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;#definell Long LongConst intM =100000+5;Const intME = 6e5 +Ten, P = -;Long LongINF =1e14;ll mx[m][p+1], mi[m][p+1];intDep[m], anc[m][p+1], fa[m], tot, h[m]; BOOLBan[me], vis[m];structedge{intV, W, NXT;} g[m<<1];structnode{intu, V, W, id;} G[me];voidAddintUintVintW) {g[++TOT].V =v, g[tot].nxt = H[u], G[TOT].W = W, h[u] =tot;}BOOLCMP (node A, node B) {returnA.W <B.W;}intFindintu) {returnU = = fa[u]? U:fa[u] =find (Fa[u]);}voidDfsintUintFintW) {Vis[u]=1; Dep[u]= Dep[f] +1; anc[u][0] = f, mx[u][0] = W, mi[u][0] = -inf;  for(intp =1; P <= p; p++) {Anc[u][p]= anc[anc[u][p-1]][p-1]; MX[U][P]= Max (mx[u][p-1], mx[anc[u][p-1]][p-1]); MI[U][P]= Max (mi[u][p-1], mi[anc[u][p-1]][p-1]); if(mx[u][p-1] > mx[anc[u][p-1]][p-1]) Mi[u][p]= Max (Mi[u][p], mx[anc[u][p-1]][p-1]); Else if(mx[u][p-1] < mx[anc[u][p-1]][p-1]) Mi[u][p]= Max (Mi[u][p], mx[u][p-1]); }     for(inti = H[u]; I i =g[i].nxt) {        intv =g[i].v; if(v = = f)Continue;    DFS (V, u, G[I].W); }}intGetlca (intUintv) {    if(Dep[u] <Dep[v]) Swap (U, v); intt = Dep[u]-Dep[v];  for(intp =0; T t>>=1, p++)        if(t&1) U =Anc[u][p]; if(U = = v)returnu;  for(intp = p; P >=0; p--)        if(Anc[u][p]! =anc[v][p]) u= Anc[u][p], V =Anc[v][p]; returnanc[u][0];} llGet(intUintVintW) {LL ret= -inf;  for(intp = p; P >=0; p--)        if(Dep[anc[u][p]] >=Dep[v]) {            if(w = = Mx[u][p]) ret =Max (ret, mi[u][p]); ElseRET =Max (ret, mx[u][p]); U=Anc[u][p]; }    returnret;}intMain () {Long LongAns =0, ret =inf; intN, M, U, V, W; scanf ("%d%d", &n,&m);  for(inti =1; I <= m; i++) {scanf ("%d%d%d", &u, &v, &W); G[i]=(node) {u, V, W, i}; } memset (mx[1],0x8f,sizeof(mx[1])); memset (mi[1],0x8f,sizeof(mi[1])); Sort (g+1, g+1+m, CMP);  for(inti =1; I <= N; i++) Fa[i] =i;  for(inti =1; I <= m; i++){        intU = g[i].u, v =g[i].v; if(Find (u) = = Find (v))Continue; Fa[find (u)]=Find (v); Ans+ = 1ll*G[I].W;        Add (U, V, G[I].W), add (V, U, G[I].W); Ban[i]=1; } DFS (1,0,0);  for(inti =1; I <= N; i++)if(!vis[i])return!puts ("Orz");  for(inti =1; I <= m; i++){        if(Ban[i])Continue; intU = g[i].u, v =g[i].v; intLCA =Getlca (U, v); ll S=Get(U, LCA, G[I].W); LL P=Get(V, LCA, G[I].W); RET= Min (ret, 1LL*G[I].W-Max (S, p)); } printf ("%lld\n", ans+ret); }

P4180 "Template" strictly sub-niche into a tree [BJWC2010]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.