题:https://codeforces.com/contest/570/problem/D

题意:给定一个以1为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到1号节点路径上的点数.每次询问a,b查询以a为根的子树内深度为b的节点上的字母重新排列之后是否能构成回文串.

分析:很明显是个树上启发式合并。显然,只要深度为bb结点的所有颜色中,至多有一种的数量为奇数就可以构成回文串了。

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int M=5e5+;
int sz[M],son[M],deep[M],w[M],countt[M][],ans[M],vis[M];
char s[M];
vector<int>g[M];
vector<pair<int,int> >Q[M]; void dfs1(int u,int fa){
sz[u]=;
deep[u]=deep[fa]+;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]])
son[u]=v;
}
}
void update(int u,int k){
countt[deep[u]][w[u]]+=k;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(!vis[v])
update(v,k);
}
}
void dfs2(int u,int sign){
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v!=son[u])
dfs2(v,);
}
if(son[u])
dfs2(son[u],),vis[son[u]]=;
update(u,);
for(int i=;i<Q[u].size();i++){
int num=;
int id=Q[u][i].first;
int d=Q[u][i].second;
for(int j=;j<;j++){
if(countt[d][j]&)
num++;
}
if(num>)
ans[id]=;
else
ans[id]=;
}
vis[son[u]]=;
if(!sign)
update(u,-);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int u,i=;i<=n;i++){
scanf("%d",&u);
g[u].pb(i);
}
scanf("%s",s);
for(int i=;i<n;i++)
w[i+]=s[i]-'a';
for(int u,v,i=;i<=m;i++){
scanf("%d%d",&u,&v);
Q[u].pb(make_pair(i,v));
}
dfs1(,);
dfs2(,);
for(int i=;i<=m;i++)
if(ans[i])
printf("Yes\n");
else
printf("No\n");
return ;
}

Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点的更多相关文章

  1. Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和

    Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...

  2. Codeforces Codeforces Round #316 (Div. 2) C. Replacement set

    C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...

  3. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  4. Codeforces Round #316 (Div. 2)

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

  6. Codeforces Round #316 (Div. 2) C. Replacement

    题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...

  7. Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

    E. Pig and Palindromes   Peppa the Pig was walking and walked into the forest. What a strange coinci ...

  8. Codeforces Round #316 (Div. 2) B. Simple Game

    思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...

  9. 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 ...

随机推荐

  1. 大数据高可用集群环境安装与配置(05)——安装zookeeper集群

    1. 下载安装包 登录官网下载安装包 https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 2. 执行命令下载并安装 cd /usr/local ...

  2. 字符串匹配之BF算法

    1)算法原理 BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等 ...

  3. 数据类型和C#关系对应

    sqlserver与c#中数据类型的对应关系///private string changetocsharptype(string type){string reval=string.empty;sw ...

  4. 已知root用户密码并登录,修改mysql用户名密码方法

    首先进入命令行下:以root用户登录,命令:mysql -uroot -p 回车 输入密码:mysql>use mysql:mysql>UPDATE user SET password=P ...

  5. Invalid bean definition with name 'dataSource' defined in class path resource [applicationContext.xml]

    启动tomcat,访问一个web项目失败,查看日志,发现异常信息: 18-Jul-2019 15:22:16.822 严重 [main] org.apache.catalina.core.Standa ...

  6. vSphere Replication5.5安装

    vSphere Replication5.5概述 VMware vSphere Replication简称VR是 VMwarevCenter Server 的扩展,提供基于管理程序的虚拟机复制和恢复功 ...

  7. redis常用命令--strings

    strings常用命令: set key value:存值 get key:取值 append key value:在值后追加值 strlen key:获取长度 mset key1 value1 ke ...

  8. MySQL数据库的数据类型

    1.整数型 2.日期和时间类型 3.字符串类型

  9. 牛客——Rabbit的字符串

    题目: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 Rabbit得到了一个字符串,她的好朋 ...

  10. Python说文解字_Python之多任务_05

    问:在Py3.5之前yield表现非常好,在Py3.5之后为了将予以变得更加明确,就引入了async和await关键词用于定义原生的协议. 答:async和await原生协程: async def d ...