[传送门](http://codeforces.com/contest/570/problem/D)
题意:
一棵树,询问某棵子树指定深度的点能否构成回文


当然不用dsu on tree也可以做

dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制....

一开始dfs没有判断重儿子T了一次

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
#define pii pair<int, ll>
#define MP make_pair
#define fir first
#define sec second
const int N=5e5+5;
int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
return x*f;
} int n, a[N], Q, x;
char s[N];
vector<pii> q[N];
int ans[N];
struct edge{int v, ne;}e[N<<1];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
}
int size[N], mx[N], deep[N], big[N];
void dfs(int u) {
size[u]=1;
for(int i=h[u];i;i=e[i].ne) {
deep[e[i].v] = deep[u]+1;
dfs(e[i].v);
size[u] += size[e[i].v];
if(size[e[i].v] > size[mx[u]]) mx[u] = e[i].v;
}
} pii f[N];
void update(int u, int val) {
f[ deep[u] ].fir += val;
f[ deep[u] ].sec ^= (1<<(a[u]));
for(int i=h[u];i;i=e[i].ne) if(!big[e[i].v]) update(e[i].v, val);
}
inline int one(int n) {
int ans=0;
while(n) n&=(n-1), ans++;
return ans;
}
inline int cal(int d) {return f[d].sec == 0 || (one(f[d].sec) == 1 && (f[d].fir & 1) );}
void dfs(int u, int keep) {
for(int i=h[u];i;i=e[i].ne)
if(e[i].v != mx[u]) dfs(e[i].v, 0);
if(mx[u]) dfs(mx[u], 1), big[mx[u]]=1;
update(u, 1);
for(int i=0; i<(int)q[u].size(); i++) ans[q[u][i].sec] = cal(q[u][i].fir);
big[mx[u]]=0;
if(!keep) update(u, -1);
} int main() {
//freopen("in","r",stdin);
n=read(); Q=read();
for(int i=2; i<=n; i++) ins(read(), i);
scanf("%s",s+1);
for(int i=1; i<=n; i++) a[i] = s[i]-'a';
for(int i=1; i<=Q; i++) x=read(), q[x].push_back(MP(read(), i));
deep[1] = 1; dfs(1);
dfs(1, 1);
for(int i=1; i<=Q; i++) puts(ans[i] ? "Yes" : "No");
}

CF 570D. Tree Requests [dsu on tree]的更多相关文章

  1. CF 600E. Lomsat gelral(dsu on tree)

    解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...

  2. Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)

    题目链接 题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串. 思路:暴力dsu找一边 简直就是神技! #include<bits/stdc++.h> #defi ...

  3. CF 208E. Blood Cousins [dsu on tree 倍增]

    题意:给出一个森林,求和一个点有相同k级祖先的点有多少 倍增求父亲然后和上题一样还不用哈希了... #include <iostream> #include <cstdio> ...

  4. HDU6504 Problem E. Split The Tree【dsu on tree】

    Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 ...

  5. 初涉DSU on tree

    早先以为莫队是个顶有用的东西,不过好像树上莫队(不带修)被dsu碾压? dsu one tree起源 dsu on tree是有人在cf上blog上首发的一种基于轻重链剖分的算法,然后好像由因为这个人 ...

  6. dsu on tree入门

    先瞎扯几句 说起来我跟这个算法好像还有很深的渊源呢qwq.当时在学业水平考试的考场上,题目都做完了不会做,于是开始xjb出题.突然我想到这么一个题 看起来好像很可做的样子,然而直到考试完我都只想出来一 ...

  7. CF 570 D. Tree Requests

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

  8. Codeforces 570D - Tree Requests(树上启发式合并)

    570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...

  9. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]

    D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...

随机推荐

  1. HDU 1241 DFS

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. Win10没有以太网图标如何找回?以太网适配器不见了怎么恢复?

    Win10以太网适配器不见了怎么恢复?以太网其实就是Win7系统中常说的"本地连接"假若用户发现网络适配器中的以太网适配器图标不见了,可以在设备管理器中添加一些这类适配器,具体过程 ...

  3. UEP-标签

    这里的标签都是常用不好理解的: formatfunc="showFormatNumer" 显示数字在页面上 ockedcolumnnum="6" 几列是不动的 ...

  4. Spark算子--flatMapValues

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/e7a46cecc65720997392516d553d9891.html flatMapValues--Tra ...

  5. 4种常用扒站工具(webzip、ha_TeleportPro、Offline Explorer、wget)

    许多开始做lead,EMU的新手,需要一个英文网站,找人定制费用太贵自己又不会技术的话,可以通过扒站来获得英文站,今天给大家介绍几种扒站工具: 1.HA_TeleportPro: Teleport P ...

  6. [SinGuLaRiTy] 复习模板-高精度模板

    [SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...

  7. 在MAC电脑上抓取iphone数据包的方法

    一.说明: 1.整个抓包操作的过程中,手机必须一直通过USB链接MAC电脑 2.手机系统要求在IOS5以上,因为使用的RVI技术在IOS5以后的系统中才有 3.抓包过程中,手机可以使用任何网络2G.3 ...

  8. VS code注释快捷键

    注释:        先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U

  9. Hive总结(七)Hive四种数据导入方式

  10. Java泛型范例

    class Point<T>{ // 此处可以随便写标识符号,T是type的简称 private T var ; // var的类型由T指定,即:由外部指定 public T getVar ...