codeforces 570 D. Tree Requests (dfs)
题目链接:
题目描述:
给出一棵树,有n个节点,1号节点为根节点深度为1。每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变成一个回文串?
解题思路:
判断是不是回文串,可以统计集合中出现过的字母的个数,出现奇数次的字母个数小于1,即为回文串,否则不是。所以我们可以使用状压统计当前区间中字母出现的奇偶次数。
对于如何快速的求出区间,先dfs整棵树,标记下来每个节点进栈的时间和出栈的时间,然后把高度一样的点按照进栈时间顺序升序存在一起。如果节点x的时间戳为(s, e),那么以x为根节点的子树中所有的节点的时间戳都在这个区间内,不在这个子树中的节点的时间戳都不在这个区间内。辣么我们就可以二分高度为h的节点寻找时间戳在(s,e)区间内的节点咯。
思路是这样的,但是就是一直Runtime error on test 15,有没有很神奇。然后就去tokers的博客膜拜一下,最后代码几乎改成一样的了,但是Runtime error on test 15一直执着的不愿离我而去。一怒之下,全部删光光,重写一边就AC了(┭┮﹏┭┮重写大法好..........)
#include <bits/stdc++.h>
using namespace std;
const int maxn = ; int L[maxn], R[maxn], rp[maxn];
int dfn, dep[maxn];
char str[maxn];
vector < int > sum[maxn];
vector < int > node[maxn];
vector < int > tree[maxn]; void dfs (int u)
{
L[u] = ++dfn;
rp[L[u]] = u;
node[dep[u]].push_back(L[u]);
for (int i=; i<tree[u].size(); i++)
{
int v = tree[u][i];
dep[v] = dep[u] + ;
dfs (v);
}
R[u] = dfn;
} void init (int n)
{
dfn = ;
dep[] = ;
for (int i=; i<maxn; i++)
{
sum[i].clear();
node[i].clear();
tree[i].clear();
}
} int main ()
{
int n, m;
while (scanf ("%d %d", &n, &m) != EOF)
{
init (n);
for (int i=; i<=n; i++)
{
int v;
scanf ("%d", &v);
tree[v].push_back(i);
} dfs ();
scanf ("%s", str+);
for (int i=; i<=n; i++)
{
int size = node[i].size();
for (int j=; j<size; j++)
{
sum[i].push_back();
int x = str[rp[node[i][j]]] - 'a';
sum[i][j] |= (<<x);
}
for (int j=; j<size; j++)
sum[i][j] ^= sum[i][j-];
} int x, h;
while (m --)
{
scanf ("%d %d", &x, &h);
int l, r, size;
l = L[x];
r = R[x];
size = node[h].size(); if (dep[x]>=h || !size || r<node[h][] || l>node[h][size-])
{
printf ("Yes\n");
continue;
} int a = lower_bound(node[h].begin(), node[h].end(), l) - node[h].begin();
int b = lower_bound(node[h].begin(), node[h].end(), r) - node[h].begin(); if (b == size || node[h][b]>r)
b --;
int res = sum[h][b];
if (a)
res ^= sum[h][a-]; int ans = ;
for (int i=; i<; i++)
{
if (res & (<<i))
ans ++;
} printf ("%s\n", ans>?"No":"Yes");
}
}
return ;
}
codeforces 570 D. Tree Requests (dfs)的更多相关文章
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces 570 D Tree Requests
题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- 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 dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
随机推荐
- NoSQL之Memcached
一.Memcached概念 Memcached是NoSQL产品之中的一个,是一个暂时性键值存储NoSQL数据库,过去被大量使用在互联网站点中,作为应用和数据库之间的缓存层,大大提高查询和訪问速度. M ...
- win10 powershell 验证下载的包的MD5/sha1的签名值
巧用Win10自带的PowerShell命令校验文件的Hash值(MD5.SHA1/256等) 发表于2017年3月8日由MS酋长 通常为了保证我们从网上下载的文件的完整性和可靠性,我们把文件下载下来 ...
- Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>
这是因为架包没导对或者关联的项目不是在同一个工作空间.
- js性能优化之函数节流(分流函数)
函数节流的原理 比如我们在window.onresize事件中要打印当前浏览器窗口的大小,在我们通过拖拽来改变窗口大小时候,打印窗口大小这个工作1s就运行了10次.而实际上我们只需要2次或者3次. 比 ...
- Scrum 时间估算
在新公司里,不懂软件工程的产品经理经常逼迫研发人员作出很不靠谱的时间估算.常见场景有下面这些: 需求未细化的情况下要求给出时间估算:比如,就一句话描述需要做一个什么样的功能,但是具体页面长什么样,交互 ...
- the first week study
1.In 1989, a man named Guido create "python" as a kind of computer languages. And now we u ...
- properties文件读取配置信息
public static void main(String[] args){ String printerName=""; String path = "C:\\Bar ...
- JavaScript对象(复习笔记)
js对象 对象构造器 function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname ...
- C项目案例实践(0)-语言基础
1.C语言数据类型 所谓数据类型是按被定义变量的性质.表示形式.占据存储空间的多少.构造特点来划分的.C中数据类型可分为基本数据类型.构造数据类型.指针类型.空类型4大类,结构如图: (1)基本数据类 ...
- JDBC连接数据库查询信息的步骤(提取成配置文件方式)
硬编码格式的弊端:数据库发生改变时,要重新修改代码,重新编译和部署 解决方法:将数据库信息写在配置文件当中,让程序通过读取配置文件来获得这些信息 jdbc.driver.class=com.mysql ...