[NOIP2015] Transport plan D2 T3 lca+ Two-point answer + difference score Group

Source: Internet
Author: User

[NOIP2015] Transport plan D2 t3description

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 integers n, m, representing the number of planets in the country and the number of transport plans that the small P company has pre-connected, and the planets numbered 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 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.

Output

A total of 1 lines, containing 1 integers, indicating the minimum time required for a small P logistics company to complete a phased operation.

Sample Input6 to 2 6, 1, 3, 5, 5Sample Output OneHINT

"Sample description"
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.

"Data size and conventions"
The scope and characteristics of all test data are shown in the following table

Solution: First preprocess all the paths of the LCA and the time spent, and then the two answers. Find all the paths that are larger than mid, so if we turn the edge I into a wormhole, make sure that all paths over mid are past I, and that the lengths of these paths are smaller than mid when I becomes a wormhole. So use the difference score group to process how many of these paths are on each side of the line.

In order to reduce the constant, I also made a few small optimizations in the code, may play a useful

#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;Const intmaxn=300010;intN,m,cnt,maxx,minn;inthead[maxn],to[maxn<<1],next[maxn<<1],val[maxn<<1];intDEEP[MAXN],SON[MAXN],TOP[MAXN],FA[MAXN],SIZE[MAXN],DIS[MAXN],V[MAXN];intS[MAXN],Q[MAXN],P1[MAXN],P2[MAXN],PA[MAXN],LEN[MAXN];intReadin ()//read-in optimization{    intret=0;CharGC;  while(gc<'0'|| Gc>'9') gc=GetChar ();  while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret;}voidAddintAintBintc) {to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;}voidDFS1 (intx) {Size[x]=1, q[++q[0]]=x;  for(inti=head[x];i!=-1; i=Next[i]) {        if(to[i]!=Fa[x]) {V[to[i]]=val[i],deep[to[i]]=deep[x]+1, dis[to[i]]=dis[x]+val[i],fa[to[i]]=x;            DFS1 (To[i]); SIZE[X]+=Size[to[i]]; if(Size[to[i]]>size[son[x]]) son[x]=To[i]; }    }}intGetlca (intXintY//Tree Profile{     while(top[x]!=Top[y]) {        if(deep[top[x]]<Deep[top[y]]) swap (x, y); X=Fa[top[x]]; }    if(deep[x]>Deep[y]) swap (x, y); returnx;}intCheckintSTA) {    inti,sum=0; memset (s),0,sizeof(s));  for(i=1; i<=m;i++)        if(len[i]>STA) S[p1[i]]++,s[p2[i]]++,s[pa[i]]-=2, sum++;  for(i=n;i>=1; i--) {S[fa[q[i] ]+=S[q[i]]; if(v[q[i]]>=maxx-sta&&s[q[i]]==sum)return 1; }    return 0;}intMain () {n=readin (), m=Readin (); memset (Head,-1,sizeof(head)); intI,j,a,b,c;  for(i=1; i<n;i++) {a=readin (), B=readin (), c=Readin (); Add (A,b,c), add (B,A,C), Minn=Max (MINN,C); } deep[1]=top[1]=1; DFS1 (1);  for(i=1; i<=n;i++)//Using DFS sequencing    {        if(Q[i]==son[fa[q[i]]) top[q[i]]=Top[fa[q[i]]; Elsetop[q[i]]=Q[i]; }     for(i=1; i<=m;i++)//pretreatment{P1[i]=readin (), p2[i]=Readin (); Pa[i]=Getlca (P1[i],p2[i]); Len[i]=dis[p1[i]]+dis[p2[i]]-2*Dis[pa[i]]; Maxx=Max (maxx,len[i]); }    intl=maxx-minn,r=maxx+1, Mid;//Narrow the range     while(l<r) {Mid=l+r>>1; if(Check (mid)) r=mid; ElseL=mid+1; } printf ("%d", R); return 0;}

[NOIP2015] Transport plan D2 T3 lca+ Two-point answer + difference score Group

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.