码迷,mamicode.com
首页 > 其他好文 > 详细

51nod1513

时间:2017-09-27 20:38:28      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:ack   space   its   air   main   max   color   read   har   

题解:

更据题意,在树上深度为没一个数的都放在一起,要用的时候二分出来,看结果

用c++的数据结构

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10,L=27;
char s[N];
int n,m,cnt=0,max_h=0,high[N];
vector<int>E[N],A[N],B[N];
pair<int,int>order[N];
void dfs(int x,int h)
{
    max_h=max(h,max_h);
    high[x]=h;
    order[x].first=++cnt;
    A[h].push_back(1<<(s[x-1]-a));
    B[h].push_back(cnt);
    for (int i=0;i<E[x].size();i++)dfs(E[x][i],h+1);
    order[x].second=cnt;
}
int solve(int x,int h)
{
    if (h<=high[x]||!B[h].size())return 1;
    int l=(int)(lower_bound(B[h].begin(),B[h].end(),
    order[x].first)-B[h].begin());
    int r=(int)(upper_bound(B[h].begin(),B[h].end(),
    order[x].second)-B[h].begin()-1);
    if (l>r)return 1;
    int ans=A[h][r]^(l?A[h][l-1]:0);
    if (ans==(ans&(-ans)))return 1;
    return 0;
}
int read()
{
    char c=0;int x=0;
    for(;c<0||c>9;c=getchar());
    for(;c>=0&&c<=9;c=getchar())x=x*10+c-0;
    return x;
}
int main()
{
    n=read();m=read();
    for (int i=2;i<=n;i++)E[read()].push_back(i);
    scanf("%s",&s);
    dfs(1,0);
    max_h++;
    for (int i=0;i<max_h;i++)
     for (int j=1;j<A[i].size();j++)A[i][j]^=A[i][j-1];
    while (m--)
     {
        int v=read(),h=read();
        if (solve(v,h-1))puts("Yes");
        else puts("No");
     } 
}

 

51nod1513

标签:ack   space   its   air   main   max   color   read   har   

原文地址:http://www.cnblogs.com/xuanyiming/p/7603410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!