CodeForces 570D 【dfs序】
题意:
给一颗树,根节点深度为1,每一个节点都代表一个子母。
数据输入:
节点数 询问数
从编号为2的节点开始依次输入其父节点的编号(共有节点数减1个数字输入)
字符串有节点数个小写字母
接下来询问
a b
代表以a为根节点的子树在深度为b(包含)的范围内所有节点的字母能否组成回文串。
能输出Yes,不能输出No
思路:
1.dfs序,对于每个节点,我们在深度和字母组成的二维数组里边记录进入节点和离开节点的时间戳。
2.用到upper_bound和lower_bound函数对该深度之下各个时间的字母数量进行统计。
3.由字母组成回文串的关键是最多有一个字母出现奇数次。
/*************************************************************************
> File Name: G.cpp
> Author: ttpond
> Created Time: 2015-8-16 16:42:27
************************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int N=5E5+;
int now=;
vector<int>tree[N];
vector<int>ans[N][];
int start[N];
int endd[N];
char str[N];
int n,m;
void dfs(int deep,int num)
{
now++;
ans[deep][str[num]-'a'].push_back(now);
start[num]=now;
vector<int>::iterator it;
for(it=tree[num].begin(); it!=tree[num].end(); it++)
{
dfs(deep+,*it);
}
endd[num]=now;
}
int main()
{
scanf("%d%d",&n,&m);
//printf("%d %d\n",n,m);
int tmp;
for(int i=; i<=n; i++)
{
scanf("%d",&tmp);
tree[tmp].push_back(i);
}
getchar();
scanf("%s",str+);
//puts(str+1);
dfs(,);
int v,h;
int ttt,rel=;
for(int i=; i<m; i++)
{
rel=;
scanf("%d%d",&v,&h);
//printf("%d %d\n",v,h);
for(int i=; i<; i++)
{
ttt=upper_bound(ans[h][i].begin(),ans[h][i].end(),endd[v])-lower_bound(ans[h][i].begin(),ans[h][i].end(),start[v]);
//printf("%d\n",ttt);
if(ttt&)
{
rel+=;
if(rel>)
break;
}
}
if(rel>)
printf("No\n");
else
printf("Yes\n");
}
}
CodeForces 570D 【dfs序】的更多相关文章
- CodeForces 570D DFS序 树状数组 Tree Requests
参考九野巨巨的博客. 查询一个子树内的信息,可以通过DFS序转成线形的,从而用数据结构来维护. #include <iostream> #include <cstdio> #i ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- Tree Requests CodeForces - 570D (dfs水题)
大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...
- Codeforces 396C (DFS序+线段树)
题面 传送门 题目大意: 给定一棵树,每个点都有权值,边的长度均为1,有两种操作 操作1:将节点u的值增加x,并且对于u的子树中的任意一个点v,将它的值增加x-dist(u,v)*k, dist(u, ...
- Codeforces 1110F(DFS序+线段树)
题面 传送门 分析 next_id = 1 id = array of length n filled with -1 visited = array of length n filled with ...
- Codeforces 1132G(dfs序+线段树)
题面 传送门 分析 对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲.为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树. 由于序 ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到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 离散DFS序 特殊的子树统计 (暴力出奇迹)
题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...
随机推荐
- 我来解数独(附delphi源码)
前段时间看到“69岁农民3天破解世界最难数独游戏”,然后在看了那个号称世界最难的数独题目之后,就打算抽空编程解决.今晚抽出一个晚上,大约四五个小时的时间,中间还间歇在clash of clans上造兵 ...
- qt QTableView/QTableWidget样式设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7591409.html 选中设置: QTableView::item:selected { background ...
- Android从图库选择照片
从机里取照片,开头用网上找的代码测试,导致类似下面这样的Crash:java.lang.RuntimeException: Failure delivering result java.lang.Se ...
- Docker下redis的主从配置
1.拉取redis镜像docker pull redis2.启动3个redis容器服务,分别使用到6379.6380.6381端口docker run --name redis-6379 -p 637 ...
- promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示
promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- TWaver可视化编辑器的前世今生(一)电信网管编辑器
走到今天,TWaver,一个致力于在技术领域(Technology)的弄潮儿(Waver),已经是第十二个年头.当年网吧的小网管都是IDC机房的运维人员了,TWaver也见证了这个时代的成长变迁. 萌 ...
- [kuangbin带你飞]专题五 并查集
并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...
- Sdoi2014 向量集
题目描述 题解: 码力太差重构之后才$A……$ 首先求向量点积最大很容易想到凸包, 设已知$(x_0,y_0)$,求$(x,y)$满足$(x,y)*(x_0,y_0)>=(x',y')*(x_0 ...
- 配置c3p0-config.xml数据库连接池,jdbcurl配置项报错Type The reference to entity "useUnicode" must end with the ';' delimiter.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <c3p0-confi ...