题目链接

题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串。

思路:暴力dsu找一边 简直就是神技!

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const int N=500007;
const ll mod=1e9+7;
struct node{
int h,id;
bool f;
friend bool operator < (node a,node b){
return a.id<b.id;
}
};
vector<int> G[N];
char val[N];
int dp[N],son[N],cnt[N][26];
int odd[N];
int de[N];
int po=0;
vector<node> h[N];
node ans[N];
void dfs(int u,int fa,int deep){
dp[u]=1;
de[u]=deep;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u,deep+1);
dp[u]+=dp[v];
if(dp[v]>dp[son[u]]) son[u]=v; //树剖
}
}
void add(int u,int fa,int w,int deep){
cnt[deep][val[u]-'a']+=w;
if(cnt[deep][val[u]-'a']&1) odd[deep]++;
else odd[deep]--;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa||v==po) continue;
add(v,u,w,deep+1);
}
}
int num=0;
void dfss(int u,int fa,int opt,int deep){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
if(v!=son[u]) dfss(v,u,0,deep+1);
}
if(son[u]) dfss(son[u],u,1,deep+1),po=son[u];
add(u,fa,1,deep);
po=0;
for(int i=0;i<h[u].size();i++){
node tmp=h[u][i];
if(odd[tmp.h]>=2) tmp.f=0;
ans[++num]=tmp;
}
if(!opt) add(u,fa,-1,deep);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m; cin>>n>>m;
for(int i=2;i<=n;i++){
int fa; cin>>fa;
G[fa].push_back(i);
}
for(int i=1;i<=n;i++){
cin>>val[i];
}
for(int i=1;i<=m;i++){
int vi,hi; cin>>vi>>hi;
node tmp; tmp.h=hi; tmp.f=1; tmp.id=i;
h[vi].push_back(tmp);
}
dfs(1,0,1);
dfss(1,0,1,1);
sort(ans+1,ans+1+m);
for(int i=1;i<=num;i++){
if(ans[i].f) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}

Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)的更多相关文章

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

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

    官方题解是离线询问,dfs树形转线性,然后二分找区间. 还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案. 代码是学 ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #316 (Div. 2)

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  6. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  7. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  8. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  9. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

随机推荐

  1. 使用javafx开发一款桌面个性化软件

    本来笔者只是打算开发一个显示在桌面的cpu和内存监控工具,没想到迭代了几次版本之后变成了桌面个性化工具了. 目前实现功能: cpu和内存的实时监控 开机自动启动 自定义logo 自定义主题颜色 鼠标拖 ...

  2. 利用GPU实现大规模动画角色的渲染(转)

    原文: https://www.cnblogs.com/murongxiaopifu/p/7250772.html 利用GPU实现大规模动画角色的渲染 0x00 前言 我想很多开发游戏的小伙伴都希望自 ...

  3. 按装parallels tool的失败之路

    这是一篇对于其他人来说没什么意义的博客.单纯的可以被看作是日记. 首先,我想安装parallels tool. 但是照着网上很多教程(如www.cnblogs.com/artwalker/p/1323 ...

  4. 四:WEB源码扩展

    前言:WEB源码在安全测试中是非常重要的信息来源,可以用来进行代码审计漏洞也可以用来做信息突破口,其中WEB源码有很多技术需要简明分析,获取某ASP源码后就可以采用默认数据库下载为突破,获取某其他脚本 ...

  5. 【Oracle】密码文件相关

    Oracle数据库的orapwd命令,主要用来建立密码(口令)文件. 一.查看帮助信息 [oracle@oracle11g dbs]$ orapwd Usage: orapwd file=<fn ...

  6. ctfhub技能树—信息泄露—备份文件下载—vim缓存

    打开靶机 查看页面信息 在使用vim时会创建临时缓存文件,关闭vim时缓存文件则会被删除,当vim异常退出后,因为未处理缓存文件,导致可以通过缓存文件恢复原始文件内容 以 index.php 为例:第 ...

  7. linux自定义安装位置安装jdk

    注:本文系参考网络内容及本人实践得出 1 下载jdk安装包 下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downl ...

  8. 量子化学Gaussian技术实战课 2021年4月9号--12号 远程在线教学

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  9. Markdown 编辑器+同步预览+文件笔记管理+静态博客 metadata 管理

    Leanote: 1. 笔记管理, 支持富文本, markdown, 写作模式.... 编辑器绝对好用. 另外特意为coder制作了一个贴代码的插件, 真是太贴心(因为作者也是coder) 2. 博客 ...

  10. GRPC Health Checking Protocol Unavailable 14

    https://github.com/grpc/grpc/blob/master/doc/health-checking.md GRPC Health Checking Protocol Health ...