LA 5061 LCA Tarjan algorithm

Source: Internet
Author: User
Tags cas

Main topic:

Given all the points of the weights are 0, given a tree, each query requires a given two x, Y and a weight of W, the X, Y path requires a bit of weight plus w, and finally find the value of each node

Because there are so many queries and points, we want to update the values of the nodes one at a time.

We can think of this, every time we find the nearest common ancestor of X, Y, we will val[x] +=w, val[y]+=w, val[lca]-=w;

At the end of the DFS, constantly update the Val value from the bottom up, let the Father plus all the son's Val value, then the LCA minus a W, the last 2 end will be added two W, and finally the equivalent of adding a W

But because the LCA adds a W, it will affect the higher ancestors, so we are considering the val[father[lca]]-=w, so that with the LCA passed the W will cancel each other, then the ancestors will not affect

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 #defineN 501007 intFa[n], father[n];8 9 intFindintx) {returnx==fa[x]?x:fa[x]=find (Fa[x]);}Ten intFirst[n], K; One structedge{ A     inty, Next; -}e[n<<1]; -  the voidAdd_edge (intXinty) - { -E[k].y = y, E[k].next =First[x]; -FIRST[X] = k++; + } -  + int_first[n], _k; A structqedege{ at     inty, Next, W, LCA; -}qe[n<<1]; -  - voidAdd_que_edge (intXintYintW) - { -Qe[_k].y = y, Qe[_k].next = _first[x], QE[_K].W = W, qe[_k].lca=0; in_FIRST[X] = _k++; - } to BOOLVis[n]; +  - voidTarjan (intU,intf) the { *Vis[u] =true, Fa[u]=u, father[u] =F; $      for(intI=first[u]; ~i; I=E[i].next) {Panax Notoginseng         intv =e[i].y; -         if(Vis[v])Continue; the Tarjan (V, u); +FA[V] =u; A     } the      for(intI=_first[u]; ~i; I=Qe[i].next) { +         intv =qe[i].y; -         if(Vis[v]) { $             intLCA =Find (v); $Qe[i].lca = qe[i^1].lca =LCA; -         } -     } the } - Wuyi intVal[n], N, m; the  - voidDfsintU,intf) Wu { -      for(intI=first[u]; ~i; I=E[i].next) { About         intv =e[i].y; $         if(v = = f)Continue; - Dfs (v, u); -Val[u] + =Val[v]; -     } A } +  the intMain () - { $    //freopen ("In.txt", "R", stdin); the     intcas, x, Y, W; thescanf"%d", &CAs); the      for(intI=1; I<=cas; i++){ thescanf"%d", &n); -memset (First,-1,sizeof(first)); ink=0; thememset (_first,-1,sizeof(_first)); the_k=0; About          for(intj=1; J<n; J + +){ thescanf"%d%d", &x, &y); thex + +, y++; the Add_edge (x, y); + Add_edge (y, x); -         } thescanf"%d", &m);Bayi          for(intj=0; J<m; J + +){ thescanf"%d%d%d", &x, &y, &W); thex + +, y++; - Add_que_edge (x, Y, W); - Add_que_edge (y, X, W); the         } thememset (Vis,0,sizeof(Vis)); theTarjan (1,1); theMemset (Val,0,sizeof(Val)); -          for(intj=0; J<_k; j+=2){ theVAL[QE[J].LCA]-=QE[J].W; the             if(Qe[j].lca!=father[qe[j].lca]) Val[father[qe[j].lca] "-=QE[J].W; theVAL[QE[J].Y] + = QE[J].W, val[qe[j^1].Y] + =QE[J].W;94          //cout<<j<< "<<qe[j].y<<" "<<qe[j^1].y<<" "<<qe[j].w<<" < <qe[j].lca<< "" <<fa[qe[j].lca]<<endl; the         } theDfs1,0); theprintf"Case #%d:\n", i);98          for(intj=1; J<=n; J + +) printf ("%d\n", Val[j]); About     } -     return 0;101}

LA 5061 LCA Tarjan algorithm

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.