[Tree chain split] Jzoj 5914 chief's worries

Source: Internet
Author: User

Description Lake by the N-gate (2≤n≤100,000, numbering from 1 to n) composed of the N-1 between these gates to connect them together, each road to "ruler" for the unit to measure, the martial arts leader found any two of the door can be directly or indirectly through the path connection 。
Although the whole river is able to reach each other, but he feared that there is a bad man to destroy the stability of the martial arts, destruction of the path, so the martial arts leader and secretly built M Secret Road (1≤m≤100,000), but each path is not more than 1 billion feet away.
Sure enough, a recently called "Too my" organization intends to destroy the path of the martial arts, please help the chief to think of ways, if the door send a to the gate B straight Path is destroyed, from A to B all the way, through the secret path of the least how much?
input first row number N M
Next N-1 line, two integers a B per line, indicates a straight path between a-a
The next M line, three digits a B V per line, indicates that a A-V channel output N-1 line, corresponding to the original input path, each path is destroyed, the minimum need to go through how long Secret road? If there are no alternative roads, output-1
Sample Input
6 34 11 34 51 26 53 6 82 3 76 4 5
Sample Output
87575
Data constraint30%: n<=300,m<=1000
50% Data: n<=1000,m<=1000
70% Data: n<=5000,m<=5000
For another 15% data points: The tree is a chain
100% Data: n,m<=100,000

Analysis

The problem is clearly the value of the edge that passes through the two ends of the secret path to the LCA.

It's not just a tree-chain split.

Write a range change and leave

What the? And check the set of practices? No

#include <iostream>#include<cstdio>#include<cstring>#defineRep (i,a,b) for (i=a;i<=b;i++)using namespacestd;Const intn=1e5+Ten;structNode {intSZ,F,S,SEG,TOP,DEP;} A[n];intScnt,rev[n];structEdge {intU,v,nx;} g[2*N];intCnt,list[n];intt[4*n],lz[4*N];intmn;intn,m;voidADD (intUintV) {g[++cnt].u=u;g[cnt].v=v;g[cnt].nx=list[u];list[u]=CNT;}voidDFS1 (intXintf) {a[x].f=f;a[x].dep=a[f].dep+1; a[x].sz=1;  for(intI=list[x];i;i=g[i].nx)if(g[i].v!=f) {DFS1 (g[i].v,x); A[x].sz+=A[g[i].v].sz; if(A[G[I].V].SZ&GT;A[A[X].S].SZ) a[x].s=g[i].v; }}voidDFS2 (intXintf) {intson=A[x].s; if(son) {a[son].seg=++scnt; REV[SCNT]=Son; A[son].top=A[x].top;    DFS2 (SON,X); }     for(intI=list[x];i;i=g[i].nx)if(!a[g[i].v].top) {a[g[i].v].seg=++scnt; REV[SCNT]=g[i].v; A[g[i].v].top=g[i].v;    DFS2 (G[I].V,X); }}voidBuild (intXintLintr) {if(l==R) {T[x]=2147483647; return; }    intMid=l+r>>1; Build (x<<1, L,mid); Build ((x<<1)+1, mid+1, R); T[X]=2147483647;}voidPushdown (intx) {if(Lz[x]) {t[x<<1]=lz[x<<1]=min (!lz[x<<1]?2147483647:lz[x<<1],lz[x]); t[(x<<1)+1]=lz[(x<<1)+1]=min (!lz[(x<<1)+1]?2147483647: lz[(x<<1)+1],lz[x]); LZ[X]=0; }}voidChange_in_segment (intXintLintRintValintllintRR) {    if(LL&GT;RR)return; if(l>rr| | LL&GT;R)return; if(ll<=l&&r<=RR) {T[x]=min (t[x],val); LZ[X]=min (!lz[x]?2147483647: Lz[x],val); return;    } pushdown (x); intMid=l+r>>1; if(MID&GT;=LL) Change_in_segment (x<<1, L,MID,VAL,LL,RR); if(MID&LT;RR) Change_in_segment ((x<<1)+1, mid+1, R,VAL,LL,RR); T[X]=min (t[x<<1],t[(x<<1)+1]);}voidQuery_in_segment (intXintLintRintllintRR) {    if(LL&GT;RR)return; if(r<ll| | L&GT;RR)return; if(ll<=l&&r<=RR) {MN=min (mn,t[x]); return;    } pushdown (x); intMid=l+r>>1; if(Ll<=mid) Query_in_segment (x<<1, L,MID,LL,RR); if(mid+1&LT;=RR) query_in_segment ((x<<1)+1, mid+1, R,LL,RR);}voidQuery_in_tree (intXinty) {intfx=a[x].top,fy=A[y].top;  while(fx!=FY) {        if(a[fx].dep<a[fy].dep) Swap (x, y), swap (FX,FY); Query_in_segment (1,1, scnt,a[fx].seg,a[x].seg); X=a[fx].f;fx=A[x].top; }    if(a[x].dep>a[y].dep) Swap (x, y); Query_in_segment (1,1, scnt,a[x].seg+1, a[y].seg);}voidChange_in_tree (intXintYintval) {    intfx=a[x].top,fy=A[y].top;  while(fx!=FY) {        if(a[fx].dep<a[fy].dep) Swap (x, y), swap (FX,FY); Change_in_segment (1,1, scnt,val,a[fx].seg,a[x].seg); X=a[fx].f;fx=A[x].top; }    if(a[x].dep>a[y].dep) Swap (x, y); Change_in_segment (1,1, scnt,val,a[x].seg+1, a[y].seg);}intMain () {Freopen ("worry.in","R", stdin); Freopen ("Worry.out","W", stdout); inti; Chars[ -]; scanf ("%d%d",&n,&m); Rep (I,1, N-1) {        intu,v; scanf ("%d%d",&u,&v); ADD (U,V);    ADD (V,u); } DFS1 (1,0); SCNT=1; a[1].seg=a[1].top=rev[1]=1; DFS2 (1,0); Build (1,1, SCNT);  for(intI=1; i<=m;i++) {        intx,y,w; scanf ("%d%d%d",&x,&y,&W);    Change_in_tree (X,Y,W); }     for(intI=1; i<=n-1; i++) {mn=2147483647; Query_in_tree (G[i*2-1].u,g[i*2-1].v); printf ("%d\n", mn==2147483647?-1: MN); }}
View Code

[Tree chain split] Jzoj 5914 chief's worries

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.