Codeforces Round #316 Tree Requests
2015-09-12 16:41
441 查看
题目链接
题意:给一棵树,树上每个节点都有一个字母,问在某个节点的子树中,到根的深度为d的所有节点上的字母任意排列,能否形成一个回文串。
思路:将树改成线性,按访问顺序给新的标号,并记录一棵子树的起始点,以及最大的子孙节点,同时将每个节点按照其深度放进一个数组中,这样每个深度数组都是有序的,并且同一棵子树上节点一定是连续的,然后就可以用二分寻找某一子树的某个深度节点的所在范围,并通过记录前缀和的方式O(1)时间内算出答案。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 500005;
int in[M], out[M];
char str[M], s[M];
vector<int> p[M], depth[M], presum[M];
inline int lowbit(int a){
return a & (-a);
}
int idx = 0, ch[M];
void dfs(int now, int dep){
in[now] = ++idx;
s[idx] = str[now];
depth[dep].push_back(idx);
for(int i = 0; i < p[now].size(); i++)
dfs(p[now][i], dep + 1);
out[now] = idx;
}
bool judge(int a){
return (a - lowbit(a)) == 0;
}
main() {
int n, m;
scanf("%d %d", &n, &m);
for(int i = 2; i <= n; i++){
int a;
scanf("%d", &a);
p[a].push_back(i);
}
scanf("%s", &str[1]);
dfs(1, 1);
for(int i = 1; i <= n; i++){
for(int j = 0; j < depth[i].size(); j++){
int v = depth[i][j];
if(j == 0) presum[i].push_back(1 << (s[v] - 'a'));
else presum[i].push_back((1 << (s[v] - 'a')) ^ presum[i][j - 1]);
}
}
for(int i = 0; i < m; i++){
int a, b;
scanf("%d %d", &a, &b);
int l = upper_bound(depth[b].begin(), depth[b].end(), in[a] - 1) - depth[b].begin();
int r = upper_bound(depth[b].begin(), depth[b].end(), out[a]) - depth[b].begin() - 1;
if(r < 0 || l >= depth[b].size()) puts("Yes");
else {
int t1, t2;
if(l == 0) t1 = 0;
else t1 = presum[b][l - 1];
t2 = presum[b][r];
if(judge(t1 ^ t2)) puts("Yes");
else puts("No");
}
}
}
题意:给一棵树,树上每个节点都有一个字母,问在某个节点的子树中,到根的深度为d的所有节点上的字母任意排列,能否形成一个回文串。
思路:将树改成线性,按访问顺序给新的标号,并记录一棵子树的起始点,以及最大的子孙节点,同时将每个节点按照其深度放进一个数组中,这样每个深度数组都是有序的,并且同一棵子树上节点一定是连续的,然后就可以用二分寻找某一子树的某个深度节点的所在范围,并通过记录前缀和的方式O(1)时间内算出答案。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 500005;
int in[M], out[M];
char str[M], s[M];
vector<int> p[M], depth[M], presum[M];
inline int lowbit(int a){
return a & (-a);
}
int idx = 0, ch[M];
void dfs(int now, int dep){
in[now] = ++idx;
s[idx] = str[now];
depth[dep].push_back(idx);
for(int i = 0; i < p[now].size(); i++)
dfs(p[now][i], dep + 1);
out[now] = idx;
}
bool judge(int a){
return (a - lowbit(a)) == 0;
}
main() {
int n, m;
scanf("%d %d", &n, &m);
for(int i = 2; i <= n; i++){
int a;
scanf("%d", &a);
p[a].push_back(i);
}
scanf("%s", &str[1]);
dfs(1, 1);
for(int i = 1; i <= n; i++){
for(int j = 0; j < depth[i].size(); j++){
int v = depth[i][j];
if(j == 0) presum[i].push_back(1 << (s[v] - 'a'));
else presum[i].push_back((1 << (s[v] - 'a')) ^ presum[i][j - 1]);
}
}
for(int i = 0; i < m; i++){
int a, b;
scanf("%d %d", &a, &b);
int l = upper_bound(depth[b].begin(), depth[b].end(), in[a] - 1) - depth[b].begin();
int r = upper_bound(depth[b].begin(), depth[b].end(), out[a]) - depth[b].begin() - 1;
if(r < 0 || l >= depth[b].size()) puts("Yes");
else {
int t1, t2;
if(l == 0) t1 = 0;
else t1 = presum[b][l - 1];
t2 = presum[b][r];
if(judge(t1 ^ t2)) puts("Yes");
else puts("No");
}
}
}
相关文章推荐
- easyui的datagrid为何无法显示json数据
- UI 自定义视图
- iOS开发-------简单通讯录2(UISearchController)
- [Wed, 19 Aug 2015 ~ Tue, 25 Aug 2015] Deep Learning in arxiv
- 关于UIScrollView的几点总结
- Storm UI参数详解
- UIViewContentMode
- IOS开发之手势——UIGestureRecognizer 共存
- Volley框架解析(二)-----Volley及RequestQueue解析
- Android 4.4 Kitkat Phone工作流程浅析(二)__UI结构分析
- HDU 1503 Advanced Fruits(LCS+输出路径)
- IOS UILabel 设置圆角
- POJ 2533 Longest Ordered Subsequence(DP最长上升子序列O(n^2)&&O(nlogn))
- iOS UItableview加载图片的时候的优化之lazy(懒加载)模式and异步加载模式
- NGUI相关----UITexture图片边缘流光效果
- 多个大小不同的UILabel底部对齐的方法
- iphone中UINavigationBar(导航条视图)的介绍
- iOS学习之KVC中setValuesForKeysWithDictionary:
- UI:关于RGB与16进制颜色值的转换
- 【转】SoapUI5.0创建WebService接口模拟服务端