Topic Links:
570 D. Tree Requests
Title Description:
Given a tree, there are N nodes, and node 1th is the root node depth of 1. Each node has a letter instead, asking if the descendants of height h in the subtree with node x root can be changed from a new sort to a palindrome?
Problem Solving Ideas:
Judge is not a palindrome string, you can count the number of letters appearing in the set, the number of odd number of letters is less than 1, that is, palindrome string, otherwise not. So we can use the pressure to count the number of odd and even occurrences of letters in the current interval.
For how to quickly find out the interval, first DFS whole tree, mark down each node into the stack of time and the time of the stack, and then the height of the same points in the order of ascending time in the sequence of the existence together. If the timestamp of Node x is (S, e), then the timestamp of all nodes in the subtree with X as the root node is within this interval, and the timestamps of nodes not in this subtree are not within this interval. Is it spicy? We can look for nodes with a two-point height of H to find the node in the time-stamp (s,e) interval.
The idea is this, but is always runtime error on test 15, there is no magic. Then went to tokers blog worship, the last code almost changed to the same, but the runtime error on test 151 is persistent reluctant to leave me. Rage, erase all light, rewrite one side on AC (┭┮﹏┭┮ rewrite Dafa good ...)
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =500100;4 5 intL[MAXN], R[MAXN], RP[MAXN];6 intDFN, DEP[MAXN];7 CharSTR[MAXN];8Vector <int>SUM[MAXN];9Vector <int>NODE[MAXN];TenVector <int>TREE[MAXN]; One A voidDFS (intu) - { -L[u] = + +DFN; theRp[l[u]] =u; - Node[dep[u]].push_back (L[u]); - for(intI=0; I<tree[u].size (); i++) - { + intv =Tree[u][i]; -DEP[V] = Dep[u] +1; + Dfs (v); A } atR[u] =DFN; - } - - voidInit (intN) - { -DFN =0; indep[1] =1; - for(intI=1; i<maxn; i++) to { + sum[i].clear (); - node[i].clear (); the tree[i].clear (); * } $ }Panax Notoginseng - intMain () the { + intN, M; A while(SCANF ("%d%d", &n, &m)! =EOF) the { + init (n); - for(intI=2; i<=n; i++) $ { $ intv; -scanf ("%d", &v); - Tree[v].push_back (i); the } - WuyiDFS (1); thescanf ("%s", str+1); - for(intI=1; i<=n; i++) Wu { - intSize =node[i].size (); About for(intj=0; j<size; J + +) $ { -Sum[i].push_back (0); - intx = Str[rp[node[i][j]]-'a'; -SUM[I][J] |= (1<<x); A } + for(intj=1; j<size; J + +) theSUM[I][J] ^= sum[i][j-1]; - } $ the intx, H; the while(M--) the { thescanf ("%d%d", &x, &h); - intL, R, size; inL =L[x]; theR =R[x]; theSize =node[h].size (); About the if(dep[x]>=h | |!size | | r<node[h][0] || l>node[h][size-1]) the { theprintf ("yes\n"); + Continue; - } the Bayi intA = Lower_bound (Node[h].begin (), Node[h].end (), L)-Node[h].begin (); the intb = Lower_bound (Node[h].begin (), Node[h].end (), R)-Node[h].begin (); the - if(b = = Size | | node[h][b]>R) -B--; the intres =Sum[h][b]; the if(a) theRes ^= sum[h][a-1]; the - intAns =0; the for(intI=0; i< -; i++) the { the if(Res & (1<<i))94Ans + +; the } the theprintf ("%s\n", ans>1?"No":"Yes");98 } About } - return 0;101}
Codeforces 570 D. Tree Requests (DFS)