【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 ...
随机推荐
- windows安装memcached
http://www.cnblogs.com/wujuntian/p/4791220.html
- 微信小程序从零开始开发步骤(七)引入外部js 文件
上一章讲到小程序页面的四种常见的跳转的方法,这一章写如何引入一个外部的js文件,既utils文件夹的用处,其实步骤很简单: 1:准备好外部想要引入的外部文件,命名为util.js,并且填充固定的文件内 ...
- 洛谷 P2437 蜜蜂路线
P2437 蜜蜂路线 题目描述 一只蜜蜂在下图所示的数字蜂房上爬动,已知它只能从标号小的蜂房爬到标号大的相邻蜂房,现在问你:蜜蜂从蜂房M开始爬到蜂房N,M<N,有多少种爬行路线? 输入输出格式 ...
- JDBC连接池C3P0
连接池 1)传统方式找DriverManager要连接.数目是有限的. 2)传统方式的close().并没有将Connection重用.仅仅是切断应用程序和数据库的桥梁,即无发送到SQL命令到数据库端 ...
- Directx9.0 学习教程3 -图形学之创建点 线 三角形 等
1.首先 介绍点的表示方法 struct CUSTOMVERTEX { float x,y,z; }; CUSTOMVERTEX Vertices[] = { {-5.0, -5.0, 0.0}, { ...
- 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】
[144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...
- Python笔记---错误笔记
Python---错误笔记 1. Python编码问题: 我们在编写 Python 脚本时,往往会写上中文凝视. 可是有时候,当我们执行程序时.却发现例如以下错误:SyntaxError: Non-A ...
- PHP 版本简单记录
PHP 版本简单记录 PHP 博物馆 http://museum.php.net/php5/ PHP 版本发布 https://secure.php.net/release ...
- php实现遍历文件目录
php实现遍历文件目录 一.总结 1.熟悉简单:很经典的例子,多看,然后发现熟悉了很简单 二.php实现遍历目录 php实现遍历目录 代码一: //遍历目录 function iteral($path ...
- ::的类名前有个 & ,什么意思?
转载自 http://www.imooc.com/qadetail/93985 MazePerson &MazePerson::setPersonPosition(int coordinat ...