"BZOJ-3757" apple tree block tree + tree on the MO team

Source: Internet
Author: User

3757: Apple tree time limit:20 Sec Memory limit:256 MB
submit:1305 solved:503
[Submit] [Status] [Discuss] Description

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 from one to N to represent a color. There are a total of n fruit in the tree. Each apple is numbered, and the number is a positive integer between 1 and N. We use 0 to represent the roots. There will only be an Apple direct root.

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.

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?

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

The output is a total of M lines, each line contains only one integer, representing the number of colors the person should count.

Sample INPUT5 3
1 1 3) 3 2
0 1
1 2
1 3
2 4
3 5
1 4 0 0
1 4 1 3
1 4 1 2
Sample Output2
1
2
Hint0<=x,y,a,b<=nn<=500001<=u,v,coli<=nm<=100000sourcesolution

In the tree, MO team naked question, below say the tree on the team

There is nothing to say in the sequence of the team, as for the tree, the thought is the same

To the tree Dfs chunking (see BZOJ-1086 Royal Commonwealth OH), then sort the questions and then deal with it, then it is the specific implementation

As for the tree block, is to follow the DFS timestamp, so the first keyword to ask the sort is where the block, the second keyword is the timestamp

How to convert a path in a tree to one end?

First we set $s (u,v) $ to represent the set of points on $u-v$ path, $LCA (U,V) $ represents the nearest public ancestor of two points, $root $ for root

$xor $ is a collection of symmetric differences that belong only to one of the collections, and not to the elements of another collection

So find $s (u,v) =s (root,u) XOR S (ROOT,V) XOR LCA (U,V) $ (not see OH)

Is that the node has been erased two times.

Then there is $t (u,v) =s (Root, V) Xor S (Root, u) $

Observation will ask $curv$ to move to $targetv$ before and after $t (CURV, Curu) $ change:

$T (CURV, Curu) =s (Root, CURV) Xor S (Root, Curu) $

$T (Targetv, Curu) =s (Root, Targetv) Xor S (Root, Curu) $

Symmetry difference:

$T (CURV, Curu) XOR T (Targetv, Curu) = (s (root, CURV) XOR s (Root, Curu)) Xor (S (Root, Targetv) XOR s (Root, Curu)) $

Due to the commutative law of the symmetry difference, the binding law:

$T (CURV, Curu) XOR T (Targetv, Curu) = S (root, Curv) xors (Root, Targetv) $

Both sides simultaneously $xor T (CURV, Curu) $:

$T (Targetv, Curu) = T (CURV, Curu) XOR s (Root, CURV) XOR s (Root, Targetv) $

$T (Targetv, Curu) =t (CURV, Curu) XOR T (CURV, Targetv) $

In other words, when updating, $xor T (CURV, Targetv) $ on the line.

That is, the $curv$ to the $targetv$ path (except $lca (CURV, Targetv)) $ on the node, the existence of their presence can be reversed. ---------------------by VFK As for the path symmetry difference? $s (7,10) $, apparently $root=1$, $S (1,7) =\left\{1,3,7\right\},s (1,10) =\left\{ 1,4,8,10\right\}$ so $s (1,7) XOR S (1,10) =\left\{3,7,4,8,10\right\}$,$\left\{3,7,4,8,10\right\} xor LCA (7,10) =\left\{ 1,3,7,4,8,10\right\}=s (7,10) $ in blue path represents $s (1,7) $, green path represents $s (1,10) $, red path represents $s (7,10) $, yellow circle means $lca (7,10) $ The rest is the same: The movement of the inquiry between, the same principle, feel a bit good code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') ch=GetChar ();  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx;}#defineMAXN 50100#defineMAXQ 100100intN,m,fk,knum,ans,root;structedgenode{intTo,next;} edge[maxn<<1];intHead[maxn],cnt=1;voidAddintUintv) {CNT++;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].to=v;}voidInsertintUintv) {Add (u,v); add (v,u);}intdeep[maxn],father[maxn][ -],DFSX,STACK[MAXN],TOP,RT[MAXN],AN[MAXQ],P[MAXN];structpointnode{intDfs,col;} PO[MAXN];structasknode{intA,b,u,v,id; BOOL operator< (ConstAsknode & A)Const        {            if(RT[U]==RT[A.U])returnpo[v].dfs<Po[a.v].dfs; Else returnrt[u]<RT[A.U]; }}Q[MAXQ];BOOLVISIT[MAXN];intDFS (intNow ) {    intSize=0; Po[now].dfs=++DFSX;  for(intI=1; i<= -; i++)        if(Deep[now]>= (1<<i)) Father[now][i]=father[father[now][i-1]][i-1];  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=father[now][0]) {deep[edge[i].to]=deep[now]+1; father[edge[i].to][0]=Now ; Size+=DFS (edge[i].to); if(size>=FK) {Knum++;  for(intj=1; j<=size; J + +) Rt[stack[top--]]=Knum; Size=0; }} stack[++top]=Now ; returnSize+1;} intLCA (intXinty) {    if(deep[x]<Deep[y]) swap (x, y); intdd=deep[x]-Deep[y];  for(intI=0; i<= -; i++)        if(dd& (1<<i) && dd>= (1<<i)) x=Father[x][i];  for(intI= -; i>=0; i--)        if(father[x][i]!=Father[y][i]) x=father[x][i],y=Father[y][i]; if(x==y)returnXElse returnfather[x][0];}voidReserv (intx) {    if(!visit[x]) {visit[x]=1; p[po[x].col]++;if(p[po[x].col]==1) ans++;} Else{visit[x]=0; p[po[x].col]--;if(p[po[x].col]==0) ans--;}}voidWorkintUintv) {     while(u!=v)if(Deep[u]>deep[v]) reserv (u), u=father[u][0]; ElseReserv (v), v=father[v][0];}intMain () {n=read (), M=read (); fk=sqrt (n);  for(intI=1; i<=n; i++) po[i].col=read ();  for(intU,v,i=1; i<=n; i++) {u=read (), v=read (); if(!u) root=v; Else if(!V) root=u; ElseInsert (U,V);    } DFS (root); Knum++;  while(top) rt[stack[top--]]=Knum;  for(intI=1; i<=m; i++) {q[i].u=read (); Q[i].v=read (); Q[i].a=read (); Q[i].b=read (); q[i].id=i; if(po[q[i].u].dfs>Po[q[i].v].dfs) Swap (Q[I].U,Q[I].V); } sort (Q+1, q+m+1); intT=lca (q[1].u,q[1].v); Work (q[1].u,q[1].V); Reserv (T); an[q[1].id]=ans; if(p[q[1].A] && p[q[1].B] && q[1].a!=q[1].B) an[q[1].id]--;     Reserv (T);  for(intI=2; i<=m; i++) {Work (Q[i-1].u,q[i].u); Work (Q[i-1].v,q[i].v); T=LCA (Q[I].U,Q[I].V); Reserv (T); An[q[i].id]=ans; if(P[Q[I].A] && p[q[i].b] && q[i].a!=q[i].b) an[q[i].id]--;        Reserv (T); }     for(intI=1; i<=m; i++) printf ("%d\n", An[i]); return 0;}

"BZOJ-3757" apple tree block tree + tree on the MO team

Related Article

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.