codeforces 570D.Tree Requests
[题目大意]:
给定一棵树,树的每个节点对应一个小写字母字符,有m个询问,每次询问以vi为根节点的子树中,深度为hi的所有节点对应的字符能否组成一个回文串;
[题目分析]:
先画个图,可看出每次询问的所有节点都是一个从1开始bfs遍历完成的节点序列中的一段,已知深度hi的情况下,可以二分得到深度为hi的那一段的位置;
那么如何满足找到的节点必须在以vi为根的子树内这个条件?
可以想到dfs的时间戳,把1-n的数组按深度排序,深度相同的按照dfs时间戳排序;
这样就可以二分得到要求的所有节点的位置;
这样可以O(n)
分析回文串可知,如果奇数数量的字符不超过1个,那么一定能组成回文串;
用前缀和xor优化;
可以做到O(nlogn);
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iomanip>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<cmath>
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);(i)<=(n);(i)++)
#define max(x,y) ((x)<(y)?(y):(x))
#define min(x,y) ((x)<(y)?(x):(y))
#define FILE "1"
#define pii pair<int,int>
const int maxn=,inf=;
int read(){
int x=;bool flag=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')flag=;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return flag?-x:x;
}
struct node{
int y,next;
}e[maxn<<];
int n,m;
int linkk[maxn],len=,sum[maxn];
int dfs_clock=,fa[maxn];
char s[maxn];
int f[maxn];
struct Node{
int dep,pre,lat,id;
bool operator<(const Node &b)const{return dep<b.dep||(dep==b.dep&&pre<b.pre);}
}a[maxn];
void insert(int x,int y){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
}
void dfs(int x){
a[x].pre=++dfs_clock;a[x].id=x;
for(int i=linkk[x];i;i=e[i].next){
if(e[i].y==fa[x])continue;
a[e[i].y].dep=a[x].dep+;
dfs(e[i].y);
}
a[x].lat=++dfs_clock;
}
void init(){
n=read(),m=read();int x;
up(i,,n){
x=read();
insert(i,x);
insert(x,i);
fa[i]=x;
}
scanf("%s",s+);
a[].dep=;
dfs();
sort(a+,a+n+);
for(int i=;i<=n;i++)f[a[i].id]=i;
}
int find(int x,int Dep){
int left=,right=n,mid;
int L=,R=;
while(left<=right){
mid=(left+right)>>;
if(a[mid].dep<Dep)left=mid+;
else {
right=mid-;
if(a[mid].dep==Dep)L=mid;
}
}
left=,right=n,mid=;
while(left<=right){
mid=(left+right)>>;
if(a[mid].dep<=Dep){
left=mid+;
if(a[mid].dep==Dep)R=mid;
}
else right=mid-;
}
if(!L&&!R)return ;
left=L,right=R;
int u=,v=;
while(left<=right){
mid=(left+right)>>;
if(a[mid].pre>=a[f[x]].pre){
right=mid-;
if(a[mid].pre>=a[f[x]].pre&&a[mid].pre<=a[f[x]].lat)u=mid;
}
else left=mid+;
}
left=L,right=R;
while(left<=right){
mid=(left+right)>>;
if(a[mid].pre<=a[f[x]].lat){
left=mid+;
if(a[mid].pre>=a[f[x]].pre&&a[mid].pre<=a[f[x]].lat)v=mid;
}
else right=mid-;
}
if(!u||!v)return ;
int p=sum[v]^sum[u-];
int cnt=;
while(p){
if(p&)cnt++;
p>>=;
}
if(cnt>=)return ;
else return ;
}
void work(){
int x,y;
for(int i=;i<=n;i++)sum[i]=sum[i-]^(<<(s[a[i].id]-'a'));
while(m--){
x=read(),y=read();
if(find(x,y))printf("Yes\n");
else printf("No\n");
}
}
int main(){
init();
work();
return ;
}
codeforces 570D.Tree Requests的更多相关文章
- Codeforces 570D - Tree Requests(树上启发式合并)
570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...
- 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 dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
- CF 570D. Tree Requests [dsu on tree]
传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- 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 ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
随机推荐
- Eventbus 使用方法和原理分析
对于 Eventbus ,相信很多 Android 小伙伴都用到过. 1.创建事件实体类 所谓的事件实体类,就是传递的事件,一个组件向另一个组件发送的信息可以储存在一个类中,该类就是一个事件,会被 E ...
- codevs——1700 施工方案第二季
1700 施工方案第二季 2012年市队选拔赛北京 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description c国边防 ...
- 了解使用Android ConstraintLayout
说明 Google I/O 2016 上发布了 ConstraintLayout, 简直是要变革 Android 写界面方式. 于是第二天我立即找到相关文档尝试, 这是官方提供的 Codelab 项目 ...
- python的私有化
1.双下划线(__) 由双下划线開始的属性在执行时被混淆,所以直接訪问是不同意的.实际上以双下划线開始的属性 会在解释时在前面加上下划线和类名.如self.__num会被解析为self._classN ...
- C 标准库 - <locale.h>
C 标准库 - <locale.h> 简介 locale.h 头文件定义了特定地域的设置,比如日期格式和货币符号.接下来我们将介绍一些宏,以及一个重要的结构 struct lconv 和两 ...
- final 和static
一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...
- hdu 2842(矩阵高速幂+递推)
题意:一个中国环的游戏,规则是一个木棒上有n个环.第一个环是能够任意放上或拆下的,剩下的环x假设想放上或拆下必须前一个环x-1是放上的且前x-2个环所有是拆下的,问n个环最少多少次操作能够所有拆掉. ...
- 不使用while,for,if等实现加法
不使用if, while,for,switch等实现从1到10的加法 解:这里使用静态函数和静态变量实现,利用类似的方法也能够实现从1打印到1000 class TheSum{ public: The ...
- ubuntu将快捷方式复制到桌面
/usr/share/applications目录下,如果我们要创建桌面快捷方式,只需要右键-复制-桌面 就Ok
- HDU 5384 Danganronpa (2015年多校比赛第8场)
1.题目描写叙述:点击打开链接 2.解题思路:本题利用字典树解决.本题要求查找全部的B[j]在A[i]中出现的总次数.那么我们能够建立一颗字典树,将全部的B[j]插入字典树,因为一个串的全部字串相当于 ...