【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 ...
随机推荐
- Oracel 格式化日期 to_char()
select empno,ename,job,mgr,to_char(HIREDATE,'yyyy-mm-dd') as 入职日期,sal,comm,deptno from emp order by ...
- POJ 2079 Triangle 旋转卡壳求最大三角形
求点集中面积最大的三角形...显然这个三角形在凸包上... 但是旋转卡壳一般都是一个点卡另一个点...这种要求三角形的情况就要枚举底边的两个点 卡另一个点了... 随着底边点的递增, 最大点显然是在以 ...
- nodejs学习(二)--express热更新nodemon,自启动项目
一.说一下 每次修改文件,我们都需要重启服务器npm start,很麻烦,所以使用引入nodemon插件,解决这个问题,实现保存文件,即自启动刷新项目 二.直接开码 npm install nodem ...
- PythonOOP面向对象编程1
什么是对象? 对象是指现实中的物体或实体(拥有一系列变量.函数(方法)的) 什么事面向对象? 把一切看成对象(实例),让对象和对象之间建立关联关系 对象都有什么特征? 属性(名词)实例变量 姓名.年龄 ...
- 【Codeforces Round #428 (Div. 2) A】Arya and Bran
[Link]: [Description] [Solution] 傻逼题 [NumberOf WA] [Reviw] [Code] #include <bits/stdc++.h> usi ...
- UVA - 10674-Tangents
题意:给出两个圆,求它们的公切线,并依照一定格式输出 做法:模拟 代码: #include<iostream> #include<map> #include<str ...
- String类型转Long类型需要注意的问题
转自:https://blog.csdn.net/m819177045/article/details/52669785/
- JS / CSS 实现的便签记录本
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- django 简单会议室预约(6)
后台完了现在来看前端,前端用了一个bootstrap框架,看起来能好看点 先看一下文件结构:在djapp里创建了两个文件夹templates和static templates里面是要显示的页面,sta ...
- Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7(5种办法,2种正解)
刚刚写的那一篇,是网上比较主流的解决办法. 鉴于实际情况,有伙伴的机器上没有遇到这个问题,我们再探究原因,最终还有4种情况需要说明. 先说,另外一种"正解". <depend ...