NOIP2015 Transport Plan (binary +lca+ differential)

Source: Internet
Author: User

4326:NOIP2015 Transport Plan Time limit:30 Sec Memory limit:128 MB
submit:308 solved:208
[Submit] [Status] [Discuss] Description

In the 2044 A.D., humans entered the cosmic era. L State n Planets, there are n−1 two-way waterway, each channel is built between two planets, this n−1 waterway connected to all the planets of L country. Little P is in charge of a logistics company, which has many transport plans, each of which is a logistics spaceship that needs to fly from the UI number planet to Planet VI. Obviously, it takes time for the spacecraft to sail through a fairway, and for Fairway J, the time it takes for any spacecraft to pass through it is TJ, and no interference between any two ships will occur. In order to encourage scientific and technological innovation, L King agreed that small p logistics companies to participate in the country's waterway construction, that is, allow small p to a certain channel to transform adult hole, the spacecraft drove through the wormhole does not consume time. The logistics company of small P before the completion of the worm hole was pre-connected with M transport plans. After the construction of the wormhole, the M transport plan will start at the same time, with all the ships starting together. When the M transport plan is completed, the small P logistics company's phased work is completed. If the small p can freely choose which channel to transform the adult hole, try to find out the small P logistics company to complete the phased work of the shortest time is how much?

Input

The first line consists of two positive integer n,m, representing the number of planets in the L state and the number of transport plans that the small P company has pre-connected, and the number of planets from 1 to N. Next n−1 describes the construction of the waterway, where line I contains three integers ai,bi and ti, indicating that the two-way waterway is built between AI and bi two planets, and the time it takes for any spacecraft to sail through it is ti. The data is guaranteed 1≤ai,bi≤n and 0≤ti≤1000. The next M-line describes the transport plan, where line J contains two positive integers uj and VJ, indicating that the J transport plan is from planet Uj to the planet VJ. Data Assurance 1≤ui,vi≤n

Output

The output file contains only an integer that represents the shortest time the logistics company of small P needs to complete a phased operation.

Sample Input6 3
1 2 3
1 6 4
3 1 7
4 3 6
3 5 5
3 6
2 5
4 5Sample Output OneHINT


Transform the 1th channel into adult hole: The three plan time is: 11,12,11, so it takes 12.
Transform the 2nd channel into adult hole: The three plan time is: 7,15,11, so it takes 15.
Transform the 3rd channel into adult hole: The three plan time is: 4,8,11, so it takes 11.
Transform the 4th channel into adult hole: The three plan time is: 11,15,5, so it takes 15.
Transform the 5th channel into adult hole: The three plan time is: 11,10,6, so it takes 11.
Therefore, the 3rd or 5th channel to transform adult holes can make the completion of the phased work of the shortest time, it takes 11.

Ideas

Two-point +lca+ differential

The long path of all queries is first calculated, and the time is O (Mlogn). The maximum number of queries to be modified is the minimum, and the maximum path value of ML is considered as two points. For all paths that are longer than ML, record the maximum query path for MX, as long as we find the maximum common side (intersection) MXE of these paths, by judging mx-mxe and ml to adjust the interval.

How to pay? Difference. The so-called differential is the operation of an interval operation into an interval endpoint. Spread the search to the tree. Each node with a CNT, for the path (U,V), Cnt[u]++,cnt[v]++,cnt[lca (u,v)]-=2, statistics on the tree Cnt[x]=sigma{cnt[son]}, so long as the cnt[x]==1 to meet the edge on this path, The side that satisfies the Cnt[x]==tot is on the intersection of the path.

The total time is O (mlogn+ (m+n) LOGL)

Code

1#include <cstdio>2#include <vector>3#include <cstring>4#include <iostream>5#include <algorithm>6 #definefor (A,B,C) for (int a= (b); a<= (c); a++)7 using namespacestd;8  9 Const intN =5*1e5+Ten;Ten structedge{intv,w;}; One   A intn,m; - intU[n],v[n],w[n],lca[n],dist[n],val[n]; -Vector<edge>G[n]; the   - voidReadint&x) { -     CharC=getchar ();intf=1; x=0; -      while(!isdigit (c)) {if(c=='-') f=-1; C=GetChar ();} +      while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar (); -x*=F; + } A   at intSiz[n],fa[n],son[n],top[n],dep[n],dis[n]; - voidDFS1 (intu) { -siz[u]=1; son[u]=0; -      for(intI=0; I<g[u].size (); i++) { -         intv=g[u][i].v; -         if(v!=Fa[u]) { inFa[v]=u; dep[v]=dep[u]+1; -dis[v]=dis[u]+G[U][I].W; to DFS1 (v); +siz[u]+=Siz[v]; -             if(Siz[v]>siz[son[u]]) son[u]=v; the         } *     } $ }Panax Notoginseng voidDFS2 (intUintTP) { -top[u]=TP; the     if(Son[u]) DFS2 (SON[U],TP); +      for(intI=0; I<g[u].size (); i++) { A         intv=g[u][i].v; the         if(v!=fa[u]&&v!=Son[u]) DFS2 (V,V);  +     } - } $ intLCA (intUintv) { $      while(top[u]!=Top[v]) { -         if(dep[top[u]]<Dep[top[v]) swap (U,V); -u=Fa[top[u]]; the     } -     returnDEP[U]&LT;DEP[V]?u:v;Wuyi } the   - intTot,mx,mxe,cnt[n]; Wu intFind_mxe (intu) { -      for(intI=0; I<g[u].size (); i++) { About         intv=g[u][i].v; $         if(V!=fa[u]) cnt[u]+=Find_mxe (v); -     } -     if(Cnt[u]==tot) mxe=Max (Mxe,val[u]); -     intTmp=cnt[u]; cnt[u]=0; A     returntmp; + } the BOOLCanintML) { -tot=0; Mx=mxe=0; $for (I,1, m)if(dist[i]>ML) { thetot++; mx=Max (mx,dist[i]); thecnt[u[i]]++,cnt[v[i]]++,cnt[lca[i]]-=2; the     } theFind_mxe (1); -     returnmx-mxe<=ML; in } the   the intMain () { About     //freopen ("transport.in", "R", stdin); the     //freopen ("Transport.out", "w", stdout); the read (n), read (m); thefor (I,1, N-1) { + read (U[i]), read (V[i]), read (W[i]); - G[u[i]].push_back (Edge) {v[i],w[i]}); the G[v[i]].push_back (Edge) {u[i],w[i]});Bayi     } theDFS1 (1), DFS2 (1,1); thefor (I,1, N-1) { -         if(dep[u[i]]<Dep[v[i]) swap (u[i],v[i]); -val[u[i]]=W[i]; the     } the     intX,y,l=0, r=0, M; thefor (I,1, M) { the read (x), read (y); -dist[i]=dis[x]+dis[y]-2*dis[lca[i]=LCA (x, y)]; theR=max (R,dist[i]); u[i]=x,v[i]=y; the     } ther++;94      while(l<R) { theM= (l+r) >>1; the         if(Can (M)) R=m;Elsel=m+1; the     }98printf"%d", L); About     return 0; -}

Ps:bzoj will not stack overflow 233

NOIP2015 Transport Plan (binary +lca+ differential)

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.