codeforces 570 D. Tree Requests 樹狀數組+dfs搜尋序

來源:互聯網
上載者:User

連結:http://codeforces.com/problemset/problem/570/D


D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).

The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.

We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.

Roma gives you m queries, the i-th of which consists of two numbers vi, hi. Let's consider the vertices in the subtree vi located at depthhi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used. Input

The first line contains two integers n, m (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.

The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).

The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.

Next m lines describe the queries, the i-th line contains two numbers vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in thei-th query. Output

Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes). Sample test(s) input

6 51 1 1 3 3zacccd1 13 34 16 11 2
output
YesNoYesYesYes
Note

String s is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome.

Clarification for the sample test.

In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".

In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them.

In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.

In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.

In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".


題意:

告訴你一顆樹的父子關係,1節點為根,再告訴你每個點上的字母。

問 v節點 子樹(包括v節點)在第h行的所有節點的字母能否組成迴文串。


做法:

先用dfs 搜尋 把所有節點標個左標號和右標號。 這樣標號以後,每個節點 用左標號 當自己 新的標號。 然後  子樹所有節點 的新標號 肯定在 子樹根節點的 左右標號之間。

標號之後分層來做。

每層  對每個字母分別做統計。

把該層所有節點 的 左標號 在樹狀數組中+1. 然後對於該層的所有詢問 做 樹狀數組統計,(sum(rit[v])-sum(lft[v]-1))。   

如果是奇數 說明這個 字母在查詢的區間內 有奇數個。

每個查詢  最多有一個奇數個的字母,否則不能構成迴文串


#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<math.h>using namespace std;const int N = 500100;int f[N];vector<int> son[N];int id;int lft[N],rit[N];int deps[N]; int ans[N];char str[N];vector<int> G[N];//深度vector<pair<int,int> > Q[N];void dfs(int nw,int dep){lft[nw]=id++;deps[nw]=dep;G[dep].push_back(nw);for(int i=0;i<son[nw].size();i++){int to=son[nw][i];dfs(to,dep+1);}rit[nw]=id++;}int bit[2*N];int lowbit(int x){return x&(-x);}void add(int wei,int x){while(wei<=id){bit[wei]+=x;wei+=lowbit(wei);}}int sum(int wei){if(wei==0)return 0;int sum=0;while(wei>0){sum+=bit[wei];wei-=lowbit(wei);}return sum;}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=2;i<=n;i++){scanf("%d",f+i);son[f[i]].push_back(i);}scanf("%s",str+1);id=1;dfs(1,1);int dep=1;for(int i=1;i<=m;i++){int vv,hh;scanf("%d%d",&vv,&hh);dep=max(hh,dep);Q[hh].push_back(make_pair<int,int>(vv,i));}for(int i=1;i<=dep;i++){for(int j=0;j<26;j++){if(j==25)int kkk=1;for(int k=0;k<G[i].size();k++)//每個節點{if(str[G[i][k]]-'a'==j)add(lft[G[i][k]],1);}for(int k=0;k<Q[i].size();k++){int v=Q[i][k].first;int ii=Q[i][k].second;if((sum(rit[v])-sum(lft[v]-1))&1) ans[ii]++;} for(int k=0;k<G[i].size();k++)//每個節點{if(str[G[i][k]]-'a'==j)add(lft[G[i][k]],-1);}// printf("jj %d \n",j);}//printf("ii %d \n",i);}for(int i=1;i<=m;i++){if(ans[i]<=1)printf("Yes\n");elseprintf("No\n");}}return 0;}



 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.