"BZOJ-4326" Transport Plan tree chain split + tree differential + dichotomy

Source: Internet
Author: User

4326:NOIP2015 Transport Plan Time limit:30 Sec Memory limit:128 MB
submit:703 solved:461
[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 the 1th channel to transform adult hole: The three plan time-consuming is: 11,12,11, 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. SourceSolution

Requires shortest path, takes two points into account

Two-point answer mid, and then think about how to check the answer.

First, if a path m, his length is <=mid then he has no effect on the answer, if his length >mid we obviously need to delete an edge on this path. Also, if you delete this edge, the answer mid is feasible, then the deleted edge is bound to satisfy: >=dis-mid

So a second mid, you will get the path of K-bar dis>mid, so to enable mid to meet the answer, we need to delete the side should be this K-path of the intersection

The method of intersection requires only a differential marker on the tree. Last Dfs again.

Here D20 and Uoj's et will card often, the measured effect of particularly excellent card statements:

The complexity of this time is $o (NLOGN) $

The method of calculating the LCA, comparing the recommended chain profile, Tarjan

Enlighten: This kind of tree difference thought is more important, must be able to think

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespaceStd;inlineintRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx;}#defineMAXN 300010intn,m;structedgenode{intNext,to,t;} edge[maxn<<1];intHead[maxn],cnt=1; voidAddedge (intUintVintW) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].t=W;}voidInsertedge (intUintVintW) {Addedge (u,v,w); Addedge (v,u,w);}intSize[maxn],fa[maxn],deep[maxn],son[maxn],top[maxn],tim[maxn];inlinevoidDfs_1 (intNowintLast ) {Size[now]=1;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last ) {Deep[edge[i].to]=deep[now]+edge[i].t; Fa[edge[i].to]=Now ; Tim[edge[i].to]=edge[i].t;                Dfs_1 (Edge[i].to,now); Size[now]+=Size[edge[i].to]; if(Size[edge[i].to]>size[son[now]]) son[now]=edge[i].to; }}inlinevoidDfs_2 (intNowintchain) {Top[now]=chain; if(Son[now]) dfs_2 (Son[now],chain);  for(intI=head[now]; I I=edge[i].next)if(Edge[i].to!=fa[now] && edge[i].to!=Son[now]) Dfs_2 (edge[i].to,edge[i].to);} InlineintLCA (intUintv) {     while(top[u]!=Top[v]) {            if(deep[top[u]]<Deep[top[v]) swap (U,V); U=Fa[top[u]]; }    if(deep[u]>Deep[v]) swap (U,V); returnu;}structroadnode{intU,v,lca,dis; Roadnode (intu=0,intv=0): U (U), V (v) {LCA=lca (U,V); dis=deep[u]+deep[v]-(deep[lca]<<1);}} ROAD[MAXN];intDelta[maxn];inlinevoidDFS (intNowintLast ) {     for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last ) DFS (Edge[i].to,now), Delta[now]+=delta[edge[i].to];} InlineBOOLCheckintx) {    intmaxx=0, num=0;  for(intI=1; i<=n; i++) delta[i]=0;  for(intI=1; i<=m; i++)        if(road[i].dis>x) Maxx=max (maxx,road[i].dis-x), num++, delta[road[i].u]++,delta[road[i].v]++,delta[road[i].lca]-=2; if(!num)return 0; DFS (1,0);  for(intI=1; i<=n; i++)         if(Delta[i]==num && Tim[i]>=maxx)return 0; return 1;} intMain () {N=read (); m=read ();  for(intU,v,w,i=1; i<=n-1; i++) U=read (), V=read (), w=read (), Insertedge (U,V,W); Dfs_1 (1,0); Dfs_2 (1,1); intmaxx=0;  for(intI=1; i<=m; i++) Road[i]=roadnode (read (), read ()), maxx=Max (Maxx,road[i].dis);//for (int i=1; i<=m; i++) printf ("%d%d%d%d\n", Road[i].u,road[i].v,road[i].lca,road[i].dis);    intL=0, r=Maxx;  while(l<=r) {intMid= (l+r) >>1; if(Check (mid)) l=mid+1;Elser=mid-1; } printf ("%d\n", L); return 0;}

"BZOJ-4326" Transport Plan tree chain split + tree differential + dichotomy

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.