The tree on the MO team algorithm

Source: Internet
Author: User

Brief introduction

The tree team, as the name suggests, is to move the team to the tree.

We start with a topic [SDOI2018] The original topic identify spoj Count on a tree II

The topic is very clear: given a tree of $n$ nodes, each node represents an integer and asks how many different integers are on the path $u$ to $v$.

such as this without the color of the modified number of the first thought must be the tree set tree , then how to put in the sequence of the team to move to the tree?

Algorithmic Euler order

We consider what can be used to transform the problem in the tree into a sequence, the DFS sequence is OK, but the problem is not (can not make the contribution of LCA)

There is a magical thing called Euler's order.

Its core idea is: when access to the point $i$, add the sequence, and then access the $i$ subtree, when the end of the visit, and then add the $i$ sequence

Boil a chestnut, the Oura of this tree below

$1\ 2\ 3\ 4\ 4\ 5\ 5\ 6\ 6\ 3\ 7\ 7\ 2\ 1$

The tree, MO Team

What's the use of having this?

We consider the problem we are trying to solve: how many different integers are there in the path to $x$ to $y$?

Here we set $st[i]$ to indicate the time to join Euler's order when accessing to $i$, $ed [i]$ indicates the time to add Euler's order when backtracking passes through $i$

Set $st[x]<st[y]$ (i.e. visit $x$ First, then visit $y$)

Sub-situation Discussion

if $lca (x, y) = x$, then $x,y$ in a chain, then $st[x]$ to $st[y]$ this period, some points appear two times, some points did not appear, these points are not contribute to the answer, we only need to statistics show $1$ times the point is good

For example, when asked for $2,6$, $ (st[2],st[6]) =2\ 3\ 4\ 4\ 5\ 5\ 6$,$4,5$ both points appear two times, so do not count into the answer

if $lca (x, y) \not = x$, this time $x,y$ is in a different subtree, we only need to follow the above method to count the $ed[x]$ to $st[y]$ the points within this interval.

For example, when asked for $4,7$, $ (ed[4],st[7]) = 4\ 5\ 5\ 6\ 6\ 3\ 7\ $. What did you find out? That's right! We don't have statistical $lca$, so we need to $lca$.

And then it's gone, let 's start with a nice code.

Relatedprove

Here purely for the author nonsense ...

    • Why does it appear two times the point no statistic answer

The path on the tree is defined as a path with the fewest number of nodes from $x$ to $y$.

If a point $k$ appears two times, we can first access the $k$, into the $k$ subtree, and then out, then to $y$, it is clear that the $k$ is more excellent. So the points that appear two times cannot be counted into the answer

    • Why do I need to traverse from $ed[x]$ when $lca (x, y) \not =x$

Nodes from $st[x]$ to $ed[x]$ as $x$, it is clear that these nodes cannot be counted into the answer

Code

Note that we ask for the interval length of $2*n$, so the pretreatment must be looped to $2*n$!

#include <cstdio>#include<cmath>#include<algorithm>#include<vector>//#define GETCHAR () (p1==p2&& (p2= (p1=buf) +fread (Buf,1,1<<20,stdin), P1==P2)? eof:*p1++)Charbuf[1<< +], *p1 = buf, *p2 =buf;using namespacestd;Const intMAXN = 1e5 +Ten; inlineintRead () {Charc = GetChar ();intx =0, F =1;  while(C <'0'|| C >'9') {if(c = ='-') F =-1; c =GetChar ();}  while(c >='0'&& C <='9') x = x *Ten+ C-'0', C =GetChar (); returnX *F;}intN, Q;intBELONG[MAXN], block;structQuery {intL, R, ID, LCA, ans; BOOL operator< (ConstQuery &AMP;RHS)Const{        returnBelong[l] = = BELONG[RHS.L]? R < Rhs.r:belong[l] <BELONG[RHS.L]; //return belong[l] < BELONG[RHS.L];}}q[maxn];vector<int>V[MAXN];intA[MAXN], DATE[MAXN];voiddiscretization () {Sort (date+1, date + N +1); intnum = Unique (date +1, date + N +1)-Date-1;  for(inti =1; I <= N; i++) A[i] = Lower_bound (date +1, date + num +1, A[i])-date; }intDEEP[MAXN], TOP[MAXN], FA[MAXN], SIZ[MAXN], SON[MAXN], ST[MAXN], ED[MAXN], POT[MAXN], tot;voidDFS1 (intXint_fa) {Fa[x]= _FA; SIZ[X] =1; ST[X]= + + tot; Pot[tot] =x;  for(inti =0; I < v[x].size (); i++) {        intto =V[x][i]; if(Deep[to])Continue; Deep[to]= Deep[x] +1;        DFS1 (to, X); SIZ[X]+=Siz[to]; if(Siz[to] > Siz[son[x]]) son[x] =to ; } Ed[x]= ++tot; Pot[tot] =x;}voidDFS2 (intXintTOPFA) {Top[x]=TOPFA; if(!son[x])return ;    DFS2 (Son[x], TOPFA);  for(inti =0; I < v[x].size (); i++) {        intto =V[x][i]; if(Top[to])Continue;    DFS2 (to); }}intGetlca (intXinty) { while(Top[x]! =Top[y]) {        if(Deep[top[x]) <Deep[top[y]]) swap (x, y); X=Fa[top[x]]; }    returnDEEP[X] < Deep[y]?x:y;}voidDealask () { for(inti =1; I <= Q; i++) {        intx = Read (), y =read (); if(St[x] >St[y]) swap (x, y); int_lca =Getlca (x, y); Q[i].id=i; if(_lca = = x) q[i].l = St[x], q[i]. R =St[y]; ElseQ[I].L = Ed[x], Q[I].R = St[y], Q[i].lca =_lca; }}intAns, out[MAXN], USED[MAXN], HAPPEN[MAXN];voidAddintx) {if(++happen[x] = =1) ans++;}voidDelet (intx) {if(--happen[x] = =0) ans--;}voidADD (intx) {used[x]? Delet (A[x]): Add (A[x]); USED[X] ^=1;}voidMo () {sort (q+1, q + q +1); intL =1, r =0, fuck =0;  for(inti =1; I <= Q; i++) {         while(L < Q[I].L) ADD (Pot[l]), l++, fuck++;  while(L > Q[i].l) L--, Add (Pot[l]), fuck++;  while(R < Q[I].R) r++, Add (Pot[r]), fuck++;  while(R > Q[I].R) ADD (Pot[r]), R--, fuck++; if(Q[I].LCA) Add (Q[I].LCA); Q[i].ans=Ans; if(Q[I].LCA) Add (Q[I].LCA); }     for(inti =1; I <= Q; i++) out[Q[i].id] =Q[i].ans;  for(inti =1; I <= Q; i++) printf ("%d\n", out[i]);}intMain () {N= Read (); Q =read (); //block = 1.5 * SQRT (2 * N) + 1; //block = Pow (N, 0.66666666666);block =sqrt (N);  for(inti =1; I <= N; i++) A[i] = date[i] =read ();  for(inti =1; I <= N *2; i++) Belong[i] = I/block +1;    Discretization ();  for(inti =1; I <= N-1; i++) {        intx = Read (), y =read (); V[x].push_back (y);    V[y].push_back (x); } deep[1] =1; DFS1 (1,0); DFS2 (1,1);/*for (int i = 1; I <= N; i++) for (int j = 1; J <= I-1; j + +) printf ("%d%d%d\n", I, J , Getlca (i, J));*/Dealask ();    Mo (); return 0;}

The tree on the MO team 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.