题目链接:codeforces570D

正解:$dsu$ $on$ $tree$

解题报告:

  考虑这又是一类子树内的不带修改统计问题,直接上$dsu$ $on$ $tree$好咯。

  直接按上一道题的做法做,类似地存一下每个深度每种字符的出现次数,对于每个点的询问,在每个点处查询一下就好了,只需奇数的个数$<=1$即可。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 500011;
const int MAXM = 1000011;
int n,m,ecnt,first[MAXN],to[MAXM],next[MAXM],Son,ans[MAXN];
int size[MAXN],son[MAXN],deep[MAXN],cnt[MAXN][26];
char ch[MAXN];
struct node{ int x,id; }tmp;
vector<node>w[MAXN]; inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
size[x]=1;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
deep[v]=deep[x]+1;
dfs(v,x); size[x]+=size[v];
if(size[v]>size[son[x]]) son[x]=v;
}
} inline void add(int x,int fa,int val){
cnt[ deep[x] ][ ch[x]-'a' ]+=val;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa || v==Son) continue;
add(v,x,val);
}
} inline void solve(int x,int fa,bool top){
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa || v==son[x]) continue;
solve(v,x,1);
}
if(son[x])
solve(son[x],x,0),Son=son[x]; add(x,fa,1);
Son=0; //处理在x上的询问
for(int i=0,ss=w[x].size();i<ss;i++) {
tmp=w[x][i]; if(tmp.x<deep[x]) { ans[tmp.id]=1; continue; }
int cc=0,h=tmp.x;
for(int j=0;j<26;j++) if(cnt[h][j]&1) cc++;
if(cc>1) ans[tmp.id]=0;
else ans[tmp.id]=1;
} if(top)
add(x,fa,-1);
} inline void work(){
n=getint(); m=getint(); int x,y;
for(int i=2;i<=n;i++) { x=getint(); link(x,i); }
scanf("%s",ch+1);
deep[1]=1; dfs(1,0);
for(int i=1;i<=m;i++) { x=getint(); y=getint(); tmp.x=y; tmp.id=i; w[x].push_back(tmp); }
solve(1,0,1);
for(int i=1;i<=m;i++)
if(ans[i]) puts("Yes");
else puts("No");
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

codeforces570D Tree Requests的更多相关文章

  1. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

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

  2. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  4. Codeforces 570D - Tree Requests(树上启发式合并)

    570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...

  5. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  6. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  7. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  8. CF 570D. Tree Requests [dsu on tree]

    传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...

  9. D Tree Requests dfs+二分 D Pig and Palindromes -dp

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

随机推荐

  1. 【keras框架】

    更高级别的封装.更简单的api,以tensorflow.theano为后端,支持更多的平台 读取网络模型后生成网络结构图 读取 from keras.models import load_model ...

  2. 【chainer框架】【pytorch框架】

    教程: https://bennix.github.io/ https://bennix.github.io/blog/2017/12/14/chain_basic/ https://bennix.g ...

  3. centos6.8安装Discuz!X3.1(PHP论坛)

    1.首先搭建apache+mysql+php环境: 一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysq ...

  4. golang build error: syntax error: nested func not allowed

    在笔记本中写了一个简易web程序,但是编译失败,提示“syntax error: nested func not allowed” . 不明白什么意思,幸好代码量小,原来是方法的末尾的“}”丢了! p ...

  5. SpringMVC的映射器,适配器,控制器

    一.各司其职   (1)HandlerMapping映射器 根据客户端请求的url,找到处理本次请求的处理器,即将请求的路径和controller关联起来 (2)HandlerAdapter适配器 对 ...

  6. linux下python编辑器的tab补全

    vi tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcompleter ...

  7. Java中的编码乱码问题

    1. Eclipse的Run Configurations中,可以配置Console的Encoding Eclipse中使用 mvn clean package命令来执行. 设置为MS932时,下面的 ...

  8. Vim 我用的python相关插件

    1. pydoc.vim(https://github.com/vim-scripts/pydoc.vim)  查找python文档. 使用方法: :Pydoc <keyword>     ...

  9. POJ - 1511 - 两次SPFA

    这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就T了.1e6的数据量,给了8s,网上其他题解中说要用SPFA. 题意:N个点的带权有向图.每次都从1出发,要到达其余没有被访问过的一个点(发传单 ...

  10. spark-sql做ETL时遇到的两个问题

    项目中使用spark-sql来作ETL,遇到两个问题,记录一下. 问题1: spark-sql –master yarn –hiveconf load_date=`date –d ..`  -e 'i ...