题目链接

题意:对于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. MongoDB 基础手册(一)

    作者:云怀大师兄 博客园:https://www.cnblogs.com/yunhuai/ 公众号:云怀大师兄 与Mysql概念对比 说明 MySQL MongoDB 数据库 DatatBase Da ...

  2. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

  3. Java高并发与多线程(一)-----概念

    其实之前一直想专门写一篇,单独说一说Java的多线程与高并发,但是一直以来,都没有想到能够用什么比较有趣的表现形式去表达出来,而且网上充斥着很多类似的博客,有好的又不好的,有简介的有繁琐的,所以也一直 ...

  4. 每日CSS_滚动页面动画效果

    每日CSS_滚动页面动画效果 2021_1_13 源码链接 1. 代码解析 1.1 html 代码片段 <section> <h2>开 始 滑 动</h2> < ...

  5. Python requirements.txt 语法

    前言 之前一直苦于一个问题,比如一些包在Win上安装不了,比如 uvloop 但是为了提高效率,代码中必须有这个模块 在运行中可以通过 os 模块判断是否使用, 那依赖文件呢? requirement ...

  6. 【C++】《Effective C++》第九章

    杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...

  7. Ansible User 模块添加单用户并ssh-key复制

    Ansible User 模块添加单用户并ssh-key复制 1 Ansible 版本: ansible 2.9.6 config file = /etc/ansible/ansible.cfg co ...

  8. Centos7安装Jenkins和目录迁移

    Centos7安装Jenkins和目录迁移 内容: 安装Jenkins和相关的配置 尝试目录迁移,模拟磁盘空间不足 1. 安装Jenkins和配置 安装 根据Jenkins的官方安装指引,安装步骤如下 ...

  9. poj-DNA排序

    描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...

  10. BAPI_PO_CHANGE

    这两天用BAPI更改采购订单,遇到了一些问题,最后调试解决了.记录如下吧.要修改的是采购订单的物料号和批次,在网上看到其它人写过关于 BAPI_PO_CHANGE的用法,但是具体问题还要具体分析啊. ...