Codeforces Round #316 (Div. 2) D Tree Requests
官方题解是离线询问,dfs树形转线性,然后二分找区间。
还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案。
代码是学习别人的http://blog.csdn.net/squee_spoon/article/details/47666667
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+; char s[maxn]; int head[maxn],to[maxn],nxt[maxn],ecnt; void addEdge(int u,int v)
{
to[ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt++;
} int qhead[maxn],qnxt[maxn]; void init()
{
memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
} struct Query
{
int h;
int other;
bool ans;
}query[maxn]; void addQuery(int v,int i)
{
qnxt[i] = qhead[v];
qhead[v] = i;
} bool ok(int n)
{
while(n){
if(n&){
return !(n>>);
}
n>>=;
}
return true;
}
int d[maxn];
int cnt[maxn];
void dfs(int u)
{
for(int i = qhead[u]; ~i; i = qnxt[i]){
query[i].other = cnt[query[i].h];
}
cnt[d[u]] ^= (<<(s[u]-'a'));
for(int i = head[u]; ~i; i = nxt[i]){
d[to[i]] = d[u]+;
dfs(to[i]);
}
for(int i = qhead[u]; ~i; i = qnxt[i]){
query[i].ans = ok(cnt[query[i].h]^query[i].other);
}
} int main()
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++){
int fa; scanf("%d",&fa);
addEdge(fa,i);
}
scanf("%s",s+);
for(int i = ; i < m; i++){
int v; scanf("%d%d",&v,&query[i].h);
addQuery(v,i);
}
d[] = ;
dfs();
for(int i = ; i < m; i++){
query[i].ans?puts("Yes"):puts("No");
}
return ;
}
Codeforces Round #316 (Div. 2) D Tree Requests的更多相关文章
- 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 ...
- Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)
题目链接 题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串. 思路:暴力dsu找一边 简直就是神技! #include<bits/stdc++.h> #defi ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #316 (Div. 2)
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- 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 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 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 ...
随机推荐
- vector 大小
vector定义以后就最好确定大小resize(),否则在vector析构时可能出现 "double free or corruption"这样的错误
- 转载 关于启用HTTPS的一些经验分享
本文转载自 https://imququ.com/post/sth-about-switch-to-https.html 随着国内网络环境的持续恶化,各种篡改和劫持层出不穷,越来越多的网站选择了全站 ...
- 验证网站SiteMap的工具
验证网站SiteMap的在线工具 http://www.xmlvalidation.com/ 在SiteMap文件中,不能直接这样写url "http://www.obriensplast ...
- UVaLive 3902 Network (无根树转有根树,贪心)
题意:一个树形网络,叶子是客户端,其他的是服务器.现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了, 现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台. 析: ...
- Levenberg-Marquardt
c++ opencv L-M源码 http://www.shenlejun.cn/article/show.asp?id=97 什么是最优化,可分为几大类? 答:Levenberg-Marquardt ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- DMOJ IOI '17 P3 - Toy Train【拓扑排序】
传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...
- IT兄弟连 JavaWeb教程 JavaBean组件定义
JavaBean是一种可重复使用的且跨平台的软件组件.JavaBean可分为两种:一种是由用户界面的JavaBean,还有一种是没有用户界面(User Interface,UI),主要负责表示业务数据 ...
- 解决element 照片墙上传时回显问题
1.先看看样式: <el-upload class="imgList" action="1165165" list-type="picture- ...
- nacos启动
nacos下载 https://github.com/alibaba/nacos 1.执行数据库脚本 2.修改配置文件application.propertiesspring.datasource.p ...