题目链接

题意:对于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. Lesson_strange_words5

    certain 一些 the company's 公司的 implement 实现 plane 平面 sluggishly 迟缓地,缓慢地 frustrated 懊恼的 profiler 分析器,分析 ...

  2. 你一定需要知道的高阶JAVA枚举特性!

    JAVA枚举,比你想象中还要有用! 我经常发现自己在Java中使用枚举来表示某个对象的一组潜在值. 在编译时确定类型可以具有什么值的能力是一种强大的能力,它为代码提供了结构和意义. 当我第一次了解枚举 ...

  3. MySql中的有条件插入 insert where

    假设现在我们有这样的需求:当数据库中不存在满足条件的记录时,可以插入一条记录,否则程序退出.该怎么实现? 1年以上工作经验的人应该都能立即想到:去检查一下库里有没有记录,没有就插入,有就结束. int ...

  4. FastApi学习(一)

    前言 学习不止 正文 介绍 FastApi是PythonWeb框架的'新晋干员',虽然年轻但是很能打 目前已有 12k start GitHub 官网 为什么说他能打呢?它内部使用了 Python 的 ...

  5. 【SpringMVC】SpringMVC 入门

    SpringMVC 入门 文章源码 SpringMVC 基本概念 在 JavaEE 开发中,几乎全都是基于 B/S 架构的开发.在 B/S 架构中,系统标准的三层架构包括:表现层.业务层.持久层. 表 ...

  6. 剑指offer 面试题6:从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 编程思想 从前往后遍历,将值存入栈中,然后打印栈中内容即可. 编程实现 /** * struct ListNode { * ...

  7. 手把手教你搭建一个跟vue官方同款文档(vuepress)

    前言 VuePress 由两部分组成:第一部分是一个极简静态网站生成器 (opens new window),它包含由 Vue 驱动的主题系统和插件 API,另一个部分是为书写技术文档而优化的默认主题 ...

  8. appium元素识别方式实战

    github代码::  https://github.com/w550856163/App_Demo.git  tag: V1.1 Appium Inspector定位工具界面介绍:  Selecte ...

  9. MCU的心脏-晶振

    晶振是石英晶体谐振器(quartzcrystal oscillator)的简称,它被称为电路系统的心脏,它为整个系统提供"心跳".中央处理器(CPU)一切指令的执行都是建立在这个& ...

  10. SW3516中文资料书

    SW3516 是一款高集成度的快充车充芯片, 支持 A+C 口任意口快充输出, 支持双口独立限流.其集成了 5A 高效率同步降压变换器, 支持 PPS/PD/QC/AFC/FCP/SCP/PE/SFC ...