CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D
题解:
这种题,基本上容易想到DFS序。
然后,我们如果再把所有节点分层存下来,那么显然可以根据 $in[v],out[v]$ 在层内二分出一段属于 $v$ 的子树的节点。
那么我们进一步考虑,如果把一层的节点,按 $a \sim z$ 再分开来,用一个 $S[c][d]$ 数组来存所有字母为 $c$,深度为 $d$ 的节点的 $in[]$ 值。
这样一来,对于一个询问 $v,h$,就在 $S[c][h]$ 二分找到属于区间 $[in[v],out[v]]$ 的那一段,这一段的长度如果为 $r$,说明 $v$ 节点的子树中、深度为 $h$ 的、字母为 $c$ 的节点有 $r$ 个,
这个 $r$ 若为偶数,那么必然可以用来组成回文串;如果为奇数,那么最多只能一个字母的个数是奇数,否则就不能组成回文串了。
AC代码:
#include<bits/stdc++.h>
#define pb(x) push_back(x)
using namespace std;
typedef pair<int,int> P;
#define fi first
#define se second
const int maxn=5e5+; int n,m;
char c[maxn];
int d[maxn];
vector<int> G[maxn];
vector<int> S[][maxn]; int clk;
int maxd;
int in[maxn],out[maxn];
void dfs(int x,int depth)
{
in[x]=++clk;
maxd=max(maxd,depth);
S[c[x]-'a'][d[x]=depth].pb(in[x]);
for(auto y:G[x]) dfs(y,depth+);
out[x]=clk;
} int main()
{
scanf("%d%d",&n,&m);
for(int y=,x;y<=n;y++)
{
scanf("%d",&x);
G[x].pb(y);
}
scanf("%s",c+); clk=, maxd=, dfs(,); while(m--)
{
int v,h; scanf("%d%d",&v,&h);
if(d[v]>=h)
{
printf("Yes\n");
continue;
} int cnt=;
for(int i=;i<;i++)
{
int L=lower_bound(S[i][h].begin(),S[i][h].end(),in[v])-S[i][h].begin();
int R=upper_bound(S[i][h].begin(),S[i][h].end(),out[v])-S[i][h].begin();
cnt+=(R-L)%;
}
if(cnt>) printf("No\n");
else printf("Yes\n");
}
}
CodeForces 570D - Tree Requests - [DFS序+二分]的更多相关文章
- 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(树上启发式合并)
570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...
- 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 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- codeforces 570D.Tree Requests
[题目大意]: 给定一棵树,树的每个节点对应一个小写字母字符,有m个询问,每次询问以vi为根节点的子树中,深度为hi的所有节点对应的字符能否组成一个回文串: [题目分析]: 先画个图,可看出每次询问的 ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- Codeforces 620E New Year Tree(DFS序 + 线段树)
题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...
随机推荐
- Java 基础【19】代理
Java 代理(Proxy)模式与现实中的代理含义一致,如旅游代理.明星的经纪人. 在目标对象实现基础上,增加额外的功能操作,由此来扩展目标对象的功能. JavaWeb 中最常见的过滤器.Struts ...
- java AOP Before, After, AfterReturning, AfterThrowing, or Around 注解
https://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html Advice In this sectio ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- Atitit 乌合之众读后感attilax总结 与读后感结构规范总结
Atitit 乌合之众读后感attilax总结 与读后感结构规范总结 1. 背景概览与鸟瞰overview 1 1.1. 社会背景 与 历史事件背景 与历史时间背景 1 1.2. 书籍简绍 2 1. ...
- js cookie跨域设置
/** * 设置cookie方法 * @param {string} c_name cookie键值 * @param {string} value cookie值 * @param {Boolean ...
- 基于jQuery可悬停控制图片轮播代码
基于jQuery可悬停控制图片轮播代码.这是一款可悬停切换全屏轮播jQuery幻灯片.效果图如下: 在线预览 源码下载 实现的代码: <!-- 轮播广告 --> <div id= ...
- 怎样写一个PC端使用的操盘手软件(用来买卖股票,查看报表,行情)
我们想写一个操盘手软件,对于操盘而言,首先是快,然后是资料尽可能丰富,最好能看到行情,报表什么的.只是windows上写软件看似基础,实际上都不怎么好弄,用C++开发确实可以实现所有功能,估计光研发费 ...
- .net IIS MVC Rest api 跨域 PUT DELETE 404 无法使用问题解决方案
一.WebConfig配置法(system.webServer 重点是 httpProtocol handlers) http://www.jinxuliang.com/blog/article/re ...
- C#.net mysql There is already an open datareader associated with this command引发的问题
[参考]There is already an open datareader associated with this command引发的问题 我在语句中并未使用 DataReader,未何也提示 ...
- Flume的监控参数
参考 flume的http监控参数说明 普通的flume启动命令 bin/flume-ng agent -c conf -f conf/flume-conf.properties -n agent - ...