Bzoj 3757: The apple Tree (the tree in the Mo Team)

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p> <center> <center> 3757: apple tree time limit:20 Sec Memory limit:256 MB <br>submit:1327 solved:510 <br>[Submit] [Status] [discuss] </center> </center>Description<p align="center"><p align="center">God Ben an apple tree at the door of his house. The Apple tree, as a tree, is of course a tree-like structure, with each branch connecting two apples, each of which can be connected to a tree root by a path of branches, and there is only one path. Since the apple tree was Ben by god, the apples were mutated and turned into a variety of colors. We use a positive integer between 1 and N to represent a Color. There are a total of n apples on the Tree. Each apple is numbered with a positive integer number between 1 and N. We use 0 to represent the Roots. There will only be one apple connected directly to the Root.</p></p><p align="left"><p align="left">There are many people Wills Ben Home Worship God Ben. But God Ben not be able to worship CASUALLY. People who come to worship God Ben need to answer a question correctly in order to enter the house and worship God Ben. The question is, from the tree of apples numbered u, from the branches to the apples numbered v, the path of the Apple has a total number of different colors (including apple U and Apple v color)? But God Ben noticed that some people who come to worship have color blindness. specifically, A person may think that color A is a color b, then when they count the Apple color, if both the color a of the apple, and the appearance of the color B apple, the person will only be counted into the color b, but not the color a in.</p></p><p align="left"><p align="left">God Ben is a good person, he will not be a great imposition, he would accept due to color blindness caused by the wrong answer (of course, the answer in the color-blind environment also must be correct). But then God Ben will change his original color of the Program. Although this is a piece of cake for God ben, he wants to test you. Can you finish the task for God's ben?</p></p>Input inputs the first behavior two integers n and m, respectively, representing the number of apples on the tree and the number of Worshippers. The next line contains the n number, and the number I represents is the color of the Apple Coli. Next there are n rows, each containing two numbers x and y, representing a branch that is connected to Apple X and Y (or root and an apple). Next there are M lines, each line containing four integers u, v, A and b, which means that the person wants to count the number of Apple U to Apple v, and this person thinks color A is color B. If a=b=0, it means that the person does not suffer from color blindness. Output<p><p>The output is a total of M lines, each line contains only one integer, representing the number of colors the person should count.</p></p>Sample Input<span style="background:rgb(141,184,255)"><span style="background:rgb(141,184,255)">5 3<br>1 1 3) 3 2<br>0 1<br>1 2<br>1 3<br>2 4<br>3 5<br>1 4 0 0<br>1 4 1 3<br>1 4 1 2<br></span></span>Sample Output<span style="background:rgb(141,184,255)"><span style="background:rgb(141,184,255)">2<br>1<br>2<br></span></span>Hint0<=x,y,a,b<=n<br>n<=50000<br>1<=u,v,coli<=n<br>m<=100000<br>Source<p><p></p></p> <center> <center> [Submit] [Status] [discuss] </center> </center>  <br><p><p></p></p><p><p><span style="font-size:18px">The puzzle: the tree is the MO team.</span></p></p><p><p><span style="font-size:18px">First of all, the points on the tree are chunked, The block mode is the same as the previous question Bzoj 1086.</span></p></p><p><p><span style="font-size:18px">Then for each query the block with the left endpoint is the first keyword, and the Dfs order for the right endpoint is the second keyword Sort.</span></p></p><p><p><span style="font-size:18px">So how do we move the interval?</span></p></p><p><p><span style="font-size:18px">There's a magical proof on the web:</span></p></p><p><p><span style="font-size:18px"></span></p></p>Use s (v, u) to represent the set of nodes on the path of V to U. Use root to represent the root node and use LCA (v, U) to represent the nearest public ancestor of V and U. So s (v, u) = s (root, v) xor s (root, u) xor LCA (v, U) where XOR is the symmetric difference of the Set. In short, It's<span style="line-height:22px"><span style="line-height:22px">node</span></span>Occurs two Times.<br>LCA is very annoying, and then define<span style="line-height:22px"><span style="line-height:22px">T (v, u) = s (root, v) xor s (root, u)//indicates that the path of the LCA is removed from u to v</span></span><span style="line-height:22px"><span style="line-height:22px">observe the changes in T (curv, Curu) before and after moving CURV to Targetv:</span></span><span style="line-height:22px"><span style="line-height:22px">T (curv, Curu) = s (root, curv) xor s (root, curu)</span></span>T (targetv, curu) = s (root, targetv) xor s (root, curu) take symmetry difference: t (curv, curu) xor t (targetv, curu) = (s (root, Curv) xor s (root, C Uru) xor (s (root, targetv) xor s (root, curu)) due to the commutative law of the symmetric difference, the binding law: T (curv, curu) xor t (targetv, curu) = s (root, curv) xors (root, T Argetv) both sides simultaneously xor T (curv, curu): t (targetv, curu) = t (curv, curu) xor s (root, curv) xor s (root, targetv) find the last two very cool ... wow haha t (targetv, curu) = t (curv, curu) xor t (curv, targetv) (with formula phobia don't go on t_t)<br>In other words, when updating, xor T (curv, Targetv) is on the LINE. That is, the CURV to Targetv path (excluding LCA (curv, targetv)) on the nodes, the existence of them can be reversed. (when doing maintenance t, then calculate the time with the LCA on It)<p><p></p></p><p><p><span style="font-size:18px">Knowing this <span style="color:rgb(54,46,43); font-family:Arial; font-size:18px; line-height:22px">t (targetv, curu) = t (curv, curu) xor T (curv, targetv) Conclusion can be transferred each Time. </span></span></p></p><p><p></p></p><pre code_snippet_id="1676567" snippet_file_name="blog_20160509_1_4266854" name="code" class="cpp">#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define N 50003using namespace Std;int n,m,sum,top;int c[n],dfsn[n],v[n*2],next[n*2],point[n],deep[n],root, Sz,block;int fa[n][20],mi[20],vis[n],num[n],ans[n],cnt,tot,belong[n],st[n];struct Data{int a,b,u,v,id;} Q[100003];int cmp (data a,data b) {if (belong[a.u]==belong[b.u]) return Dfsn[a.v]<dfsn[b.v];return belong[a.u]< belong[b.u];} void add (int x,int y) {tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;tot++; next[tot]=point[y]; point[y]=tot; v[tot] =x;} int DFS (int x,int f) {dfsn[x]=++sz; int size=0;for (int i=1;i<=16;i++) {if (deep[x]-mi[i]<0) break;fa[x][i]=fa[fa[x][i-1]][i-1];} For (int i=point[x];i;i=next[i]) if (v[i]!=f) {deep[v[i]]=deep[x]+1; fa[v[i]][0]=x; Size+=dfs (v[i],x); If (size>=block)//sub-block {cnt++; For (int J=1;j<=size;j++) belong[st[top--]]=cnt; size=0; }}st[++top]=x;return size+1;} int LCA (int X,int y)//to Find the nearest common ancestor {if (deep[x]<deeP[y]) swap (x, y), int k=deep[x]-deep[y];for (int i=0;i<17;i++) if (k>>i&1) x=fa[x][i];if (x==y) return x;for (int i=16;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];return fa[x][0];} void Reserve (int x)//the Existence of the point is reversed {if (!vis[x]) {vis[x]=1; num[c[x]]++; if (num[c[x]]==1) sum++;} else{vis[x]=0; num[c[x]]--;if (num[c[x]]==0) sum--;}} void solve (int x,int y)//change the existence of a point on the x, y path except LCA {while (x!=y) {if (deep[x]>deep[y]) {reserve (x); x=fa[x][0];} Else{reserve (y); y=fa[y][0];}}} int main () {scanf ("%d%d", &n,&m), for (int. i=1;i<=n;i++) scanf ("%d", &c[i]); for (int i=1;i<=n;i++) {int x, y; scanf ("%d%d", &x,&y); If (!x) root=y;//because 0 is no apple, all we are looking for is an apple root else if (!y) root=x; else add (x, y); }mi[0]=1; Block=sqrt (n); for (int i=1;i<=16;i++) mi[i]=mi[i-1]*2;deep[root]=1;dfs (root,0); ++cnt;while (top) belong[st[top-- ]]=cnt;for (int i=1;i<=m;i++) {scanf ("%d%d%d%d", &q[i].u,&q[i].v,&q[i].a,&q[i].b), if (dfsn[q[i]. U]>dfsn[q[i].v]) Swap (q[i].u, q[i].v); q[i].id=i; }sort (q+1,q+m+1,cmp); int T=lca (q[1].u,q[1].v); Solve (q[1].u,q[1].v); Reserve (t); ans[q[1].id]=sum; If (num[q[1].a]&&num[q[1].b]&&q[1].a!=q[1].b) Ans[q[1].id]--;//color Blindness Special Reserve (t); For (int I=2;i<=m;i++) {solve (q[i-1].u,q[i].u); solve (q[i-1].v,q[i].v); int T=lca (q[i].u,q[i].v); Reserve (t); ans[q[i].id]=sum; If (num[q[i].a]&&num[q[i].b]&&q[i].a!=q[i].b) ans[q[i].id]--; Reserve (t); } for (int I=1;i<=m;i++) printf ("%d\n", ans[i]);}</pre><br><br><p><p></p></p><p><p><br></p></p><p><p>Bzoj 3757: The apple Tree (the tree in the Mo Team)</p></p></span>

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.