570D codeforces Round #316 (Div. 2) D (Dfs sequence, timestamp, binary

Source: Internet
Author: User

Title: Each node in a tree has a character value that asks if the characters of each node's child nodes with a depth of H can form a palindrome string.

Idea: The first is a wonderful DFS sequence and timestamp, by logging each node's DFS in and out time, you can find that a node's child nodes in and out of the time range (this is very intuitive DFS nature), so that the tree structure can be transformed into a linear structure, convenient for various processing. DFS handles timestamps over time, the depth of each node, and records the nodes of each depth, at which point each depth node is ordered. Then for a query, you can use a binary lookup to determine which segment of a child node is in that layer. For each layer, the prefix of each character is pre-processed and the number of O (1) Sany can be achieved. As a result of this problem as long as the parity, so only need to record 01, and because the character set is 26, you can directly use the type of int pressure.

PS: two points directly with the STL, the result is written like a tuo Xiang = =

#include <bits/stdc++.h>#definePB Push_back#defineSe Second#defineFS First#defineSq (x) (x) * (x)#defineEPS 0.000000001#defineLB Lower_boundusing namespaceStd;typedefLong LongLl;typedef pair<ll,ll>P;Const intmaxv=5e5+ the;intN,m;vector<int>G[maxv];vector<int>D[maxv];vector<int>NUM[MAXV];intDEEP[MAXV];int inch[MAXV], out[MAXV];CharS[MAXV];intclo=0;voidDfsintVintDintf)    {D[D].PB (v); DEEP[V]=D; inch[v]=++CLO;  for(intI=0; I<g[v].size (); i++){        intu=G[v][i]; if(u==f)Continue; DFS (U,d+1, V); }     out[v]=++Clo;}voidCul () { for(intI=1; i<=n;i++){        if(d[i].size () = =0){             Break; } NUM[I].PB (1<< (s[d[i][0]-1]-'a'));  for(intj=1; J<d[i].size (); j + +){            intc=s[d[i][j]-1]-'a'; NUM[I].PB (Num[i].back ()^(1<<c)); }    }}BOOLCmpl (Const int&va,Const int&AMP;VB) {return inch[va]<vb;}BOOLCMPR (Const int&va,Const int&AMP;VB) {return  out[va]<vb;}intcas=0;intMain () {//freopen ("/home/files/cppfiles/in", "R", stdin);Cin>>n>>m;  for(intI=2; i<=n;i++){        intp; scanf ("%d",&p);    G[P].PB (i); } scanf ("%s", s); DFS (1,1,-1);    Cul ();  while(m--){        intv,h; scanf ("%d%d",&v,&h); intL=inch[v],r= out[v],sd=h; if(deep[v]>h) {Puts ("Yes"); Continue; } Vector<int>::iterator itl=LB (D[sd].begin (), D[sd].end (), L,cmpl); Vector<int>::iterator itr=LB (D[sd].begin (), D[sd].end (), R,CMPR); if(Itr!=d[sd].begin () && (Itr!=d[sd].end () && out[*itr]>r) | | Itr==d[sd].end ())) itr--; if(Itl!=d[sd].end () &&itr!=d[sd].end () &&inch[*itl]>=l&& out[*itr]<=R) {            intdl=itl-D[sd].begin (); intdr=itr-D[sd].begin (); intNu= (dl>0? num[sd][dl-1]:0); if(nu^NUM[SD][DR]) {                if(__builtin_popcount (NU^NUM[SD][DR]) <2) puts ("Yes"); Else{puts ("No"); }            }Else{puts ("Yes"); }        }Else{puts ("Yes"); Continue; }    }}
View Code

570D codeforces Round #316 (Div. 2) D (Dfs sequence, timestamp, binary

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.