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序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...
随机推荐
- 里特定律 - Little's Law
里特定律(Little's Law)源自排队理论,是IT系统性能建模中最广为人知的定律. 里特定律揭示了前置时间(Lead Time).在制品数量(Work In Progress, WIP)和吞吐率 ...
- 移除sql数据所有链接用户
use master; go declare @temp nvarchar(20) declare myCurse cursor for select spid from sy ...
- 用JS获取Html中所有图片文件流然后替换原有链接
function displayHtmlWithImageStream(bodyHtml) { var imgReg = /<img.*?(?:>|\/>)/gi; var arr ...
- vue利用计算属性做(展开收起)小例子
<template> <div class="wrap"> <div class="box"> <div v-for= ...
- 洛谷 P1216 [USACO1.5]数字三角形 Number Triangles(水题日常)
题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...
- Keil Debug (printf) Viewer
Debug (printf) Viewer Home » µVision Windows » Debug (printf) Viewer The Debug (printf) Viewer windo ...
- python 人脸识别试水(一)
1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip pip 版本10 4.安装 numpy :pip ins ...
- 【原创翻译】链接DLL至可执行文件---翻译自MSDN
可执行文件.exe链接(或加载)DLL有以下两种形式: 隐式链接 显式链接 隐式链接是指静态加载或在程序加载时动态链接. 通过隐式链接,在使用DLL时,可执行文件链接到一个由生成DLL的人提供的导入函 ...
- windows cmd color颜色设置
上一篇我们讲了去模仿电影黑客的命令界面,其中有关于cmd命令行的颜色设置,下面就细说一下: 1.cmd进入命令 2.color+空格+? 系统会给与相关提示: 3.可以看出颜色是一个十六进制控制 ...
- hdu2795(Billboard)线段树
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...