题目链接:

  570 D. Tree Requests

题目描述:

  给出一棵树,有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)的更多相关文章

  1. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  2. codeforces 570 D Tree Requests

    题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  4. 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 ...

  5. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  6. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

  7. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

  8. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

随机推荐

  1. Javascript标准事件模型

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6676913.html 1. 分类 IE -- 冒泡型 现代 ...

  2. 十分简洁的手机浏览器 lydiabox

    没有地址栏,没有工具栏.web app无需下载.无需安装.无需更新,加入即用:再也不用记住网址.更不用输入网址--一款这样极简极方便的浏览器,你想要吗? 我们做了一个十分简洁的手机浏览器,这个浏览器也 ...

  3. Android-support-v4源码查看

  4. Lightoj 1010 - Knights in Chessboard

    1010 - Knights in Chessboard    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  5. Get started with Sourcetree

    Understand the interface Bookmarks window From that window, select the Local or Remote buttons to vi ...

  6. Webdriver中关于driver.navigate().to()和driver.get()使用的区别

    先是有一个父页上button弹开一个子页,总共有4个子页,必须前一个页上的必填信息录完,才能在这个页面触发下一个子页. 用driver.navigate().to(baseUrl2),直接跳转到第2个 ...

  7. 如何将Eclipse中的项目迁移到Android Studio中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...

  8. python 操作memercache类库

    pip install python-memcached pip install  pymemcache pip install   python-libmemcached

  9. 用jQuery插件来提升SharePoint列表表单用户体验

    本文将描述如何通过简单的CSS和jQuery插件提升SharePoint默认的列表表单体验.这些小技巧并不仅仅改善了外观,还提升了可用性. 剩余字数 我们以通知列表为例.通知正文字段假设要求不应该超过 ...

  10. 关于eclipse部署项目后,在tomcat中的webapps文件夹下没有项目

    转自:https://blog.csdn.net/yang505581644/article/details/78802316 一.发现问题 在eclipse中新建Dynamic Web Projec ...