Codeforces 165D Beard Graph Edge Tree profile + tree-like array

Source: Internet
Author: User

Beard Graph

Test instructions: give you a tree consisting of n nodes, which supports the following: 1 I: Dye the edge of the I to black (guaranteed that the edge is white at this time), 2 I: The edge of the I dyed white (guaranteed at this time the side is black), 3 a B: Find out a A, a two point only the black edge of the shortest path

Idea: Tree-chain split + tree-like array, each side of the weight to the point it points to, the initial All Black edge, black edge weight of 1, Baibeng value of-inf, Black edge to white edge, the point right increase-inf, white edge black edge point right increase INF, because it is not possible white edge white edge, so can do, At the time of the query to subtract 2 points of the nearest common ancestor of the right, the recent public ancestor can be obtained through the tree section get function, query results <0 that can not reach

AC Code:

#include"iostream"#include"string.h"#include"Stack"#include"Queue"#include"string"#include"Vector"#include"Set"#include"Map"#include"algorithm"#include"stdio.h"#include"math.h"#definell Long Long#defineBug (x) cout<<x<< "" << "Uuuuu" <<endl;#defineMem (a,x) memset (A,x,sizeof (a))#defineMP (x, y) make_pair (x, y)#definePB (x) push_back (x)using namespacestd;Const Long LongINF = 1e18+1LL;Const intINF = 1e9+1e8;Const intn=1e5+ -;Constll mod=1e9+7;intto[n<<1],nex[n<<1],head[n],tot=2;intSiz[n],son[n],tip[n],top[n],dep[n],fa[n],cnt=0;intn,m;ll C[n];map<int,int>M;voidAddintUintv) {To[tot]=v; Nex[tot]=Head[u]; Head[u]=tot++;}voidDFS1 (intUintf) {Siz[u]=1; Fa[u]=F; Dep[u]=dep[f]+1;  for(intI=head[u]; i!=-1; I=Nex[i]) {        intv=To[i]; if(v==f)Continue; M[i>>1]=v;        DFS1 (V,u); Siz[u]+=Siz[v]; if(Siz[v]>siz[son[u]]) son[u]=v; }}voidDFS2 (intUintTP) {Tip[u]=++CNT; Top[u]=TP; if(Son[u]) DFS2 (SON[U],TP);  for(intI=head[u]; i!=-1; I=Nex[i]) {        intv=To[i]; if(V!=fa[u] && v!=Son[u]) DFS2 (V,V); }}intLowbit (intx) {    return(-X) &x;}voidUpintx, ll C) {     while(x<=N) {C[x]+=C; X+=lowbit (x); }}ll sum (intLintR) {LL ret=0; L--;  while(l>0) {ret-=C[l]; L-=lowbit (L); }     while(r>0) {ret+=C[r]; R-=Lowbit (R); }    returnret;}voidGet_sum (intUintv) {ll ans=0;  while(top[u]!=Top[v]) {        if(Dep[top[u]) <Dep[top[v]) swap (U,V); Ans+=sum (Tip[top[u]], tip[u]); U=Fa[top[u]]; }    if(Dep[u] >Dep[v]) swap (U,V); U for LCA ans+=sum (tip[u],tip[v]); Ans-=sum (tip[u],tip[u]); if(ans<0) cout<<"-1\n"; Elsecout<<ans<<"\ n";}intMain () {Ios::sync_with_stdio (false), Cin.tie (0), Cout.tie (0); CIN>>N; Mem (Head,-1); intc,u,v;  for(intI=1; i<n; ++i) {cin>>u>>v;        Add (U,V);    Add (V,u); } DFS1 (1,1); DFS2 (1,1);  for(intI=2; i<=n; ++i) {Up (I,1); } CIN>>m;  while(m--) {cin>>c>>u; if(c==3) {cin>>W;        Get_sum (U,V); }        Else if(c==2) Up (tip[m[u]],-inf); Else{            //ll T=sum (1,tip[m[u]])-sum (1,tip[m[u]]-1);Up (Tip[m[u]],inf); }    }    return 0;}

Codeforces 165D Beard Graph Edge Tree profile + tree-like array

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.