HDU contest808 ACM Multi-school 7th Field Problem-1008:traffic Network in Numazu

Source: Internet
Author: User

First of all, hooves. Ranking of this game: 59

(see so many emmmm for the first time)

Okay, get into the text qaq.

... This problem ah, the idea is very clear ah.

First you see the tree on the edge of the path and then also with the modification, not obviously can think of tree section + Line tree maintenance heavy chain?

And then you see, this is a connected graph, and then there are n points n edges, so obviously there will be a ring (then a cactus is formed ...). But I don't know about cacti.

And then you look! There will only be a ring, we assume that there is no this ring, then this is a tree section template problem, then we can specifically, let this ring when the root, in addition to this ring other than the node to simple tree profile ?

Well, isn't that obvious? So we consider how to deal with that simple ring ... emmmm! Consider violence maintenance, consider violence maintenance, then we are O (n) , and the modification is O (1) .

So let's deal with the prefix and what? Unfortunately, this is just a two of the complexity of the reverse. Then we consider the complexity of the data structure optimization (neutralization).

Yes, the tree-like array to maintain the prefix and, to achieve the query and modification is O (log n) complexity (and the constant is very small), so the problem successfully contains the tree, segment tree, tree array of the three algorithms.

Cough, don't worry ah, this is not yet said how to find the ring AH.

Emmm ... Believe it or not, you think of Tarjan a second. Yes, obviously.

Then there are four algorithms.

Do you have anything else? (Isn't it enough?) Four algorithms in addition to the tree array good big other code size is very AH)

Emmm ... No, it's gone.

If you really want to say something, it's the deep search you're going to use when you mark the ring ... (In fact, can be Tarjan when the ring mark out of the good felling)

Then, then ... On the code (Error demonstration, but the thought is correct, when I revise to come back pits again)

by judge#include<iostream> #include <cstring> #include <cstdio> #define LS k<<1#define rs K  <<1|1#define Mid (l+r>>1) #define LL long longusing namespace std;const int M=1e5+111;inline int read () {int x=0,f=1;    Char C=getchar (); for (;!    IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} int n,m,pat,cnt,tim,stop;ll t[m<<2],ff[m];int Id[m],s[m],in[m],rk[m];int head[m],dfn[m],low[m],stk[m],blg[m]    ; int siz[m],dep[m],f[m],son[m],top[m],val[m];struct edge{int to,val,next,frm; Edge (int to,int val,int next,int frm): To, Val (Val), Next (next), frm (frm) {} Edge () {}}e[m<<2];inline void Add (    int U,int V,int C,int w) {E[++pat]=edge (v,c,head[u],w), Head[u]=pat; E[++pat]=edge (u,c,head[v],w), Head[v]=pat;} #define V e[i].to#define cc e[i].val/* tarjan indent processing ring */void tarjan (int u,int fa) {Dfn[u]=low[u]=++ti    M,stk[++stop]=u; for (int i=head[u];i;i=e[i].next) if (V!=FA) {if (!dfN[V]) Tarjan (v,u), Low[u]=min (Low[u],low[v]);     else Low[u]=min (Low[u],dfn[v]);        } if (Dfn[u]==low[u]) {int j=stk[stop]; if (j==u)--stop,blg[u]=u;        return; do{J=stk[stop--],id[++cnt]=j,in[j]=j;}        while (J!=U);    for (J=2;J&LT;=CNT;++J) for (int i=head[id[j]];i;i=e[i].next) if (blg[v]!=u) Add (u,v,cc,e[i].frm), in[v]=j;    }}void DFSS (int u,int fa,int stp) {ID[STP]=U,CNT=STP,DFN[U]=STP;        for (int i=head[u];i;i=e[i].next) {if (Blg[v]!=blg[u] | | v==fa) continue; if (V!=blg[u]) DFSS (v,u,stp+1);        In[u]=u; Top[v]=blg[v],val[v]=cc,s[e[i].frm]=v;    Break    }}/* Tree profile */void dfs1 (int u) {siz[u]=1, dep[u]=dep[f[u]]+1;        for (int i=head[u];i;i=e[i].next) {if (Blg[v]!=v | | v==f[u]) continue;        IN[U]?IN[V]=IN[U]:0,F[V]=U,VAL[V]=CC,DFS1 (v);    s[e[i].frm]=v,siz[u]+=siz[v],siz[v]>siz[son[u]]?son[u]=v:0;    }}void dfs2 (int u) {if (!top[u]) top[u]=u; dfn[u]=++tim,rk[tim]=u; if (!son[u]) return;    TOP[SON[U]]=TOP[U],DFS2 (Son[u]); for (int i=head[u];i;i=e[i].next) if (blg[v]==v && v!=f[u] && v!=son[u]) dfs2 (v);} #undef v#undef cc/* binary-indexed-tree */inline int lowbit (int x) {return x& (-X);} inline void Add (int x,int y) {for (; X<=n;x+=lowbit (x)) Ff[x]+y;}    inline ll get_pre (int x,int y,int z=cnt) {ll r1=0,r2=0;    for (; x;x-=lowbit (x)) r1-=ff[x];    for (; Y;y-=lowbit (y)) r1+=ff[y];    for (; Z;z-=lowbit (z)) r2+=ff[z]; return min (R1,R2-R1);} inline void Cbuild () {for (int i=1,j;i<=cnt;++i) Add (I,val[id[i]);} inline void change (int x,int y) {Add (X,-val[id[x]]), Val[id[x]]=y,add (X,val[id[x]]);}    /* Segment-tree */void build (int k,int l,int R) {if (l==r) return (void) (T[k]=val[rk[l]]); Build (Ls,l,mid), Build (Rs,mid+1,r), T[k]=t[ls]+t[rs];}    void update (int k,int l,int r,int x,int y) {if (L>x | | r<x) return; if (l==r) return (void) (t[k]=y); Update (LS,L,MID,X,Y), update (Rs,mid+1,r,x,y), T[k]=t[ls]+t[rs];}    ll query (int k,int l,int r,int l,int R) {if (L>r | | l>r) return 0; if (l<=l && r<=r) return t[k]; return query (LS,L,MID,L,R) +query (rs,mid+1,r,l,r);    }/* Tree Profile Inquiry */inline ll get_sum (int u,int v) {ll ans=0,inu=in[u],inv=in[v]; U=blg[u],v=blg[v];        while (Top[u]^top[v]) {if (Dep[top[u]]<dep[top[v])) swap (U,V);    Ans+=query (1,1,tim,dfn[top[u]],dfn[u]), u=f[top[u];        } if (U^v) {if (Dep[u]>dep[v]) swap (U,V);    Ans+=query (1,1,tim,dfn[u]+1,dfn[v]);    } if (U==id[1]) ans+=get_pre (INU,INV); return ans;}     int main () {freopen ("testdata.in", "R", stdin);    int T=read (), x,y,opt;        while (t--) {n=read (), M=read (), pat=cnt=tim=0; for (int i=1,u,v,c;i<=n;++i) in[i]=head[i]=dfn[i]=0, U=read (), V=read (), C=read (), Add (U,        V,c,i);        Tarjan (1,0), DFSS (id[1],0,1);        DFS1 (Id[1]), DFS2 (id[1]);        Cbuild (), build (1,1,tim);      while (m--) {      Opt=read (), X=read (), Y=read ();            if (opt) printf ("%lld\n", Get_sum (x, y));            else if (blg[s[x]]!=id[1]) change (dfn[s[x]],y);        else update (1,1,TIM,DFN[S[X]],Y); }}return 0;}

By the way I brushed n multi-Tree Section template problem also debug a half-hour title: The month under the "Mao Jingxiu", bzoj permission problems, but Luo Gu also have.

The problem is the Benquan on the path of the tree, and then with the modification (interval modification!) Two interval modifications! ), the code below, and then a bit of place added explanation, the other is not difficult to understand, template:

by judge#include<iostream> #include <cstdio> #define LS k<<1#define rs k<<1|1#define mid (l+r     >>1) using namespace std;const int m=1e5+111;const int inf=1e9+7;inline int read () {int x=0,f=1; char C=getchar (); for (;!    IsDigit (c); C=getchar ()) if (c== '-') f=-1;    for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} inline char cread () {char c=getchar (); while (!islower (c)) C=getchar (); return c;} int N,pat,tim;int Head[m],id[m],s[m],t[m<<2],tag[m<<2],ad[m<<2];int Siz[M],dep[M],f[M],son[M], dfn[m],top[m],val[m];struct edge{int to,val,next;}    e[m<<1];inline void Add (int u,int v,int c) {e[++pat]= (Edge) {v,c,head[u]},head[u]=pat; E[++pat]= (Edge) {u,c,head[v]},head[v]=pat;}    #define V e[i].tovoid dfs1 (int u) {siz[u]=1;        for (int i=head[u];i;i=e[i].next) {if (V==f[u]) continue;        F[v]=u,dep[v]=dep[u]+1,val[v]=e[i].val;        DFS1 (v),siz[u]+=siz[v],s[i+1>>1]=v;    if (Siz[v]>siz[son[u]]) son[u]=v; }}void dfs2 (int u) {dfn[u]=++tim,id[tim]=u; if (!top[u]) top[u]=u;    if (!son[u]) return;    TOP[SON[U]]=TOP[U],DFS2 (Son[u]); for (int i=head[u];i;i=e[i].next) if (V!=f[u] && v!=son[u]) dfs2 (v);}    #undef vinline void Build (int k,int l,int R) {if (l==r) return (void) (T[k]=val[id[l]]); Build (Ls,l,mid), Build (Rs,mid+1,r), T[k]=max (T[ls],t[rs]);} inline void pushdown (int k) {//pushdown When we consider two lazy tags at any time will only leave a one, so judge two to be good if (Tag[k]) T[ls]=t[rs]=tag[ls]=tag[rs]=ta    g[k],tag[k]=ad[ls]=ad[rs]=0;        else if (Ad[k]) {if (Tag[ls]) t[ls]=tag[ls]+=ad[k]; else ad[ls]+=ad[k],t[ls]+=ad[k]; if (Tag[rs]) t[rs]=tag[rs]+=ad[k];        else Ad[rs]+=ad[k],t[rs]+=ad[k];    ad[k]=0; }}void update_to (int k,int l,int r,int l,int r,int x) {if (L>r | | L&GT;R) return;  if (l<=l && r<=r) return (void) (tag[k]=t[k]=x,ad[k]=0); If modified, the add tag is directly void Pushdown (k), Update_to (ls,l,mid,l,r,x), update_to (rs,mid+1,r,l,r,x), T[k]=max (T[ls],t[rs]);} void Update_add (int k,int l,iNT R,int l,int r,int x) {if (L>r | | L&GT;R) return;  if (l<=l && r<=r) {if (tag[k]) t[k]=tag[k]+=x; else ad[k]+=x,t[k]+=x; return;} Interval Plus, according to whether there are modification marks to do pushdown (k), Update_add (ls,l,mid,l,r,x), Update_add (rs,mid+1,r,l,r,x), T[k]=max (T[ls],t[rs]);} int query (int k,int l,int r,int l,int R) {if (L>r | | L>r) Return-inf;    if (l<=l && r<=r) return t[k]; Pushdown (k); Return max (query (ls,l,mid,l,r), query (Rs,mid+1,r,l,r));} inline void change () {int x=dfn[s[read ()]],w=read (); update_to (1,1,tim,x,x,w);}    inline void Cover () {int u=read (), V=read (), W=read ();        while (Top[u]!=top[v]) {if (Dep[top[u]]<dep[top[v])) swap (U,V);    Update_to (1,1,TIM,DFN[TOP[U]],DFN[U],W), U=f[top[u]]; } if (u==v) return;    if (Dep[u]>dep[v]) swap (U,V); Update_to (1,1,tim,dfn[u]+1,dfn[v],w);}    inline void Add () {int u=read (), V=read (), W=read ();        while (Top[u]!=top[v]) {if (Dep[top[u]]<dep[top[v])) swap (U,V); Update_add (1,1,TIM,DFN[TOP[U]],DFN[U],w), U=f[top[u]]; } if (u==v) return;    if (Dep[u]>dep[v]) swap (U,V); Update_add (1,1,tim,dfn[u]+1,dfn[v],w);}    inline void Max () {int u=read (), V=read (), Res=-inf;        while (Top[u]^top[v]) {if (Dep[top[u]]<dep[top[v])) swap (U,V);    Res=max (Res,query (1,1,tim,dfn[top[u]],dfn[u)), U=f[top[u]];        } if (U!=v) {if (Dep[u]>dep[v]) swap (U,V);    Res=max (Res,query (1,1,tim,dfn[u]+1,dfn[v)); } printf ("%d\n", res);}    int main () {n=read (); int u,v,w,opt;    for (int i=1;i<n;++i) U=read (), V=read (), W=read (), add (U,V,W);    DEP[1]=1,DFS1 (1), DFS2 (1), build (1,1,tim);            while ((Opt=cread ())! = ' t ') switch (opt) {case ' H ': change (); Case ' O ': Cover ();            Break Case ' d ': Add ();            Break Case ' a ': Max ();        Break } return 0;}

After brushing the two questions ... The right side of the path and what (and some disgusting line-of-tree double-lazy tagging) is not a problem (theoretically)

HDU contest808 ACM Multi-school 7th Field Problem-1008:traffic Network in Numazu

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.