codeforces570D Tree Requests
题目链接: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的更多相关文章
- 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 ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- Codeforces 570D - Tree Requests(树上启发式合并)
570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- CF 570D. Tree Requests [dsu on tree]
传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...
- 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 ...
随机推荐
- Linux下桥接模式详解一
注册博客园已经好长时间,一直以来也没有在上面写过文章,都是随意的记录在了未知笔记上,今天开始本着分享和学习的精神想把之前总结的笔记逐步分享到博客园,和大家一起学习,一起进步吧! 2016-09-20 ...
- jquery prop attr
checked比较特殊,只要设置了属性checked,不管何值都是checked的.例如:<input type="checkbox" checked><inpu ...
- SharePoint 2013 附加内容数据库后出现404错误
本文讲述怎样解决SharePoint 2013 加内容数据库(Content DataBase)后出现404错误. 笔者依照http://technet.microsoft.com/en-us/lib ...
- JSP页面实现自动跳转!
JSP页面实现自动跳转!一.页面自动刷新: 把如下代码加入<head>区域中<meta http-equiv=”refresh” content=”5″>注:content=” ...
- PHP连接MYSQL操作数据库
PHP连接MYSQL操作数据库 <?php $con = mysql_connect("localhost","root",""); ...
- php等守护进程监控脚本(转载 http://www.9958.pw/post/php_script_scan)
此脚本用户守护监控进程的执行情况,因为有的时候,我们用各类开发语言做的守护进程可能会因为一些特殊情况被退出,所以此脚本就是为了重启这些进程 代码: #!/bin/bash EMAIL='9958_pw ...
- javascript 对象 原型 prototype
- openocd shell脚本
openocd.sh #! /usr/bin/expectset timeout 30spawn suexpect "密码:"send "123456\r"se ...
- NC二次开发常用的方法
//这张表存放的是所有单据模板的信息表 如果不知道单据模板的信息后可在数据库中查询PUB_BILLTEMPLET//这张表是打印模板的表改模板可以再此表修改pub_print_template//获取 ...
- 两个java小练习
网上看到的一个小练习,自己写了一个,但是时间限制并不符合,并且貌似还有些小问题,暂时放在这儿,代码格式什么的也不太规范. 1.班级排名时间限制: 1000ms 内存限制: 65536kB 描述 信息科 ...