Codeforces Round #316 (Div. 2) D、E
Problem D:
题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串
分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显然这个状态可以用二进制表示
那么对于单个询问如何快速找到所有符合要求的点呢?
这里可以考虑树的dfs序,我们把深度相同的点按照dfs序加入集合中,易知在同一颗子树中的点肯定形成了一个连续的区间。
因此每次可以通过二分子树根节点的进入dfs序的时间和出dfs序的时间来找到这个区间
找到区间后可以根据预处理出的异或前缀和直接得到这个区间的二进制状态,再check一下即可
复杂度 n+m*(logn+26)
代码:
#include <bits/stdc++.h>
using namespace std;
vector<int>g[];
int a[];
char s[];
int in[];
int out[];
int n,m;
int t=;
struct node
{
int t,val;
bool operator <(const node &a)const
{
return t<a.t;
}
};
vector<node>ans[];
void dfs(int now,int d)
{
in[now]=++t;
ans[d].push_back(node{t,ans[d][ans[d].size()-].val^a[now]});
for(int i=;i<(int)g[now].size();i++)
{
int to=g[now][i];
dfs(to,d+);
}
out[now]=++t;
}
int main()
{
scanf("%d%d",&n,&m); for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
} scanf("%s",s);
for(int i=;i<=n;i++)
{
a[i]=<<(s[i-]-'a');
ans[i].push_back(node{,});
}
dfs(,);
while(m--)
{
int p,x;
scanf("%d%d",&p,&x);
auto l=lower_bound(ans[x].begin(),ans[x].end(),node{in[p],});
auto r=upper_bound(ans[x].begin(),ans[x].end(),node{out[p],});
l--;
r--;
int st=((*l).val)^((*r).val);
int cnt=;
for(int i=;i<;i++)
{
if(st&(<<i))
cnt++;
}
if(cnt>)
{
puts("No");
}
else
{
puts("Yes");
}
}
return ;
}
Problem E:
题意:给一个n*m的棋盘,每个格子有一个字母,现在从1,1走到n,m,每次只能向右或者向下走,问走的路径上形成的字符串是回文串的方案书是多少
分析:从回文串的中点,即x+y=(n+m)/2的地方开始dp ,dp[i][j][k]存储当前回文长度为2*i ,左端点的x为i,右端点的x为j的回文串有多少个。
然后按照对角线递推即可。转移时注意必须满足左端点的x必须小于右端点,y必须大于右端点
代码:
#include <bits/stdc++.h>
using namespace std;
int n,m;
int dp[][][];
char s[][];
const int mod=1e9+;
int main()
{
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
{
scanf("%s",s[i]);
}
int ok=;
for(int i=(n+m)/-; i>=; i--)
{
memset(dp[i%],,sizeof(dp[i%]));
int j=n+m--i;
for(int x1=; x1<n; x1++)
{
int y1=i-x1;
if(y1<||y1>=m)
continue;
for(int x2=; x2<n; x2++)
{ int y2=j-x2;
if(y2<||y2>=m)
continue;
if(x1>x2||y1>y2)
continue;
if(s[x1][y1]==s[x2][y2])
{
if(ok==)
{
dp[i%][x1][x2]=;
continue;
}
for(int a=; a<; a++)
{
if(x1+a>=n)
continue;
for(int b=-; b<; b++)
{
if(x2+b<)
continue;
dp[i%][x1][x2]=(dp[i%][x1][x2]+dp[(i+)%][x1+a][x2+b])%mod;;
}
}
}
}
}
ok++;
}
printf("%d\n",dp[][][n-]);
return ;
}
Codeforces Round #316 (Div. 2) D、E的更多相关文章
- Codeforces Round #437 (Div. 2)[A、B、C、E]
Codeforces Round #437 (Div. 2) codeforces 867 A. Between the Offices(水) 题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从 ...
- Codeforces Round #298 (Div. 2) A、B、C题
题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement set
C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...
- 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 #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C
题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...
- Codeforces Round #316 (Div. 2) C. Replacement
题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
- Codeforces Round #316 (Div. 2) B. Simple Game
思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...
- Codeforces Round #604 (Div. 2) D、E、F题解
Beautiful Sequence Beautiful Mirrors Beautiful Bracket Sequence (easy version) Beautiful Sequence \[ ...
- Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点
题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点 ...
随机推荐
- jQuery实现密保互斥问题
密保互斥问题: 密保通常都会有n个问题,让用户选择其中2.3个,而且都不会让用户选择重复的问题.这就要求密保互斥. 效果如下: 下面我用了jquery实现密保互斥,用于解决密保,投票等类似互斥问题,可 ...
- 泛泰A870刷4.4专用英文版非触摸CWM Recovery 6.0.4.8(三版通刷)
首先声明. 发此Recovery的目的是測试能否够三版都能够启动. 而且不会出现像850 860之前出现过的卡第一屏问题! 不希望看到某些人士的过度解读!! 此Recovery能够刷第三方4.4 RO ...
- Laravel 4 Quick Tip: Custom Error Pages
App::error(function($exception, $code) { switch ($code) { case 403: return Response::view('errors.40 ...
- 第二节,CCSpriteBatchNode CCSpriteFrameCache
1,CCSpriteBatchNode 精灵集合类 其中Batch的英文含义是一批,一群的意思.他的对象常常包含了许多的精灵对象,这些精灵对象有一个共同的特点,那就是使用同一张文理图片.虽然是同一个纹 ...
- [Redux] Implementing Store from Scratch
Learn how to build a reasonable approximation of the Redux Store in 20 lines. No magic! const counte ...
- win7 无线网络无法启动
开始菜单-运行输入services.msc然后确定!找到WLAN Autoconfig这一项,启动此项服务,一切就OK了
- -bash: ulimit: open files: cannot modify limit: Operation not permitted
普通用户登录系统报错,提示: -bash: ulimit: open files: cannot modify limit: Operation not permitted. 处理方法: #vi /e ...
- 2015 UESTC Winter Training #4【Regionals 2008 :: Asia - Tehran】
2015 UESTC Winter Training #4 Regionals 2008 :: Asia - Tehran 比赛开始时电脑死活也连不上WIFI,导致花了近1个小时才解决_(:зゝ∠)_ ...
- JY01-KX-01
复习: 1.a标签跳转 <p id="地址"></p> <a href="#地址"></a> 预习: 1.out ...
- MTK Android 默认值修改笔记
1.设置菜单: 1.1位置信息(Location)默认关闭: 请将 alps\frameworks\base\packages\SettingsProvider\res\values\default. ...