【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 depth hi. 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 the i-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).
Examples
input
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2
output
Yes
No
Yes
Yes
Yes
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”.
【题目链接】:http://codeforces.com/contest/570/problem/D
【题解】
题意:
给你m个询问,每个询问让你在以vi为根节点的子树里面,找到深度为hi的所有节点,这些节点的节点上都有一个字符;问你这些字符(只有小写字母,可以交换顺序)能不能组成一个回文串;
做法:
利用异或的性质;
用zt[i]表示深度为i时,a-z这些字母出现次数的奇偶性(0表示偶数,1表示奇数,可以用0..2^26的二进制来表示->奇偶性的改变对应异或);
(回文串的话,显然最后二进制的zt[i]里面只能有一个1或没有1);
我用程序说
void dfs(int x,int d)
{
int len = q[x].size();
/*
在进入一个节点前;
先“知道”在没有进入它的子树前
与他有关的询问的深度为h[i]的所有节点的字符状态;
在出来的时候再异或一次,就能知道在我们遍历它的子树时,那些深度为h[i]的节点的字符状态发生了哪些变化,如果没有变化的话,最后结果就为0->表示这个子树里面没有满足要求的深度为h[i]的节点;则为空串,那样也符合题意;
*/
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
len = a[x].size();
rep1(i,0,len-1)
{
int y = a[x][i];
dfs(y,d+1);
}
zt[d]^=1<<(s[x]-'a');//更改深度为d的节点所拥有的字母的奇偶性
len = q[x].size();
rep1(i,0,len-1)//再异或一次,就能得到这一次遍历子树增加了哪些新的字母了,且可得它们的奇偶性.
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
}
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 5e5+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,m;
vector <int> a[MAXN];
vector <pii> q[MAXN];
int zt[MAXN] = {0},ans[MAXN];
char s[MAXN];
void dfs(int x,int d)
{
int len = q[x].size();
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
len = a[x].size();
rep1(i,0,len-1)
{
int y = a[x][i];
dfs(y,d+1);
}
zt[d]^=1<<(s[x]-'a');
len = q[x].size();
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
rep1(i,2,n)
{
int p;
rei(p);
a[p].pb(i);
}
scanf("%s",s+1);
rep1(i,1,m)
{
int vi,hi;
rei(vi);rei(hi);
q[vi].pb(mp(i,hi));
}
dfs(1,1);
rep1(i,1,m)
{
if (ans[i]&(ans[i]-1))// 00100-1=00011;00100&00011==0;
puts("No");
else
puts("Yes");
}
return 0;
}
【19.77%】【codeforces 570D】Tree Requests的更多相关文章
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- codeforces 570 D Tree Requests
题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
随机推荐
- Spring boot 解析jsp支持jsp热部署
解析jsp并且支持jsp热部署 参考地址:https://www.wanpishe.top/detail?blogId=39875536-7d45-48ec-a7b5-f36b85c3a235
- windows10系统window键失灵,没有反应
今天键盘的的Window键(win键)按了没反应,某度一圈全是它的垃圾营销号文章,没卵用..最后在微软官方社区支持找到解决方案.也建议大家遇到系统问题到微软社区去寻求帮助,毕竟人家是专业. 解决办法 ...
- dp之多重背包(二进制优化)
void solve(int v,int w,int c){ int count=0; for(int k=1;k<=c;k<<=1) { val[c ...
- [RxJS] Marbles Testings
Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...
- android 自己定义状态栏和导航栏分析与实现
效果 android 4.4之后,系统是支持自己定义状态栏和导航栏的.举个最典型的样例就是bilibiliclient了(iOS版本号和android版本号能用两套全然不一样符合各自系统的设计ui,良 ...
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
转自:http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html 比较的是四种复制的方式,分别为Apache的BeanUtils和Prope ...
- 过滤选择器及jQuery提供的相关方法
基本过滤器: <body> <ul> <li>列表1</li> <li class="red">列表2</li&g ...
- 【AIM Tech Round 4 (Div. 2) B】Rectangles
[链接]http://codeforces.com/contest/844/problem/B [题意] 也是道计数水题,没什么记录意义 [题解] 枚举每个点的位置在,然后往右往下 枚举和它一样颜色的 ...
- Hadoop学习总结(2)——Hadoop入门详解
1.Hadoop介绍 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台,为用户提供了一个系统底层细节透明的分布式架构,通过Hadoop,可以将大量的廉价机器的计算资源组织起来,解决单机 ...
- CentOS 6 IPv6 关闭方法
http://www.linuxidc.com/Linux/2012-06/63642.htm http://blog.csdn.net/ccscu/article/details/7814028