D Tree Requests dfs+二分 D Pig and Palindromes -dp
2 seconds
256 megabytes
standard input
standard output
Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of then - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).
The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.
We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.
Roma gives you m queries, the i-th of which consists of two numbers vi, hi. Let's consider the vertices in the subtree vi located at depthhi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.
The first line contains two integers n, m (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.
The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).
The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.
Next m lines describe the queries, the i-th line contains two numbers vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in the i-th query.
Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).
题解:把相同深度的点存入一个数组中,每个点都是当前这个点的在树中查找的时间 在询问的时候就按照深度在相应的 数组中找出 时间区间在某个区间内的点就ok了
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
using namespace std;
#define mk make_pair
#define pub push_back
const int maxn=;
int num[maxn],depth[maxn],childmaxdepth[maxn];
int in[maxn],out[maxn],tim;
vector<int>G[maxn];
char str[maxn];
vector< pair<int,int> >D[maxn];
void dfs(int cur,int de)
{
int siz=G[cur].size();
in[cur]=++tim;
depth[cur]=childmaxdepth[cur]=de;
if(D[de].size()<){
D[de].pub(mk(,));
}
int d=str[cur-]-'a';
D[de].pub(mk(in[cur],D[de].back().second^(<<d)));
for(int i=; i<siz; i++)
{
int to=G[cur][i];
dfs(to,de+);
childmaxdepth[cur]=max(childmaxdepth[cur],childmaxdepth[to]); }
out[cur]=++tim;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
tim=;
for(int i=; i <= n; i++)
{
int d;
scanf("%d",&d);
G[d].push_back(i);
}
scanf("%s",str);
dfs(,);
for(int i=; i<m; i++)
{
int val,c;
scanf("%d%d",&val,&c);
if(childmaxdepth[val]<c||c<=depth[val])
{
puts("Yes");continue;
}
int L=lower_bound(D[c].begin(),D[c].end(),
mk(in[val],-))-D[c].begin();
int R=lower_bound(D[c].begin(),D[c].end(),
mk(out[val],-))-D[c].begin();
int t=D[c][L-].second^D[c][R-].second;
int ok=t-(t&(-t));
if(ok)puts("No");
else puts("Yes");
} }
return ;
}
E
给了一个矩阵 每个格子中有一个字符 然后要求从11 走到 nm 点 是一个回文串 只能从 向右或者向下走
我们枚举步数,然后 dp[x1][x2]可以得到 现在的点 (x1,y1), (x2,y2),然后我们再次使用利用滚动数组可以得到想要的 从之前的那些点得到
//向左向右 add(dp[cur][x1][x2],dp[cur^1][x1][x2]);
//向左向上 add(dp[cur][x1][x2],dp[cur^1][x1][x2+1]);
//向下向右 add(dp[cur][x1][x2],dp[cur^1][x1-1][x2]);
//向下向上 add(dp[cur][x1][x2],dp[cur^1][x1-1][x2+1]);
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=;
long long dp[][maxn][maxn];
char str[maxn][maxn];
const int mod=;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
scanf("%s",str[i]);
if(n+m<=){
printf("%d\n",str[][]==str[n-][m-]); return ;
}
if(str[][]!=str[n-][m-]){
printf("%d\n",);return ;
}
for(int i=; i<n; i++)
for(int j=; j<m; j++)dp[][i][j]=;
dp[][][n-]=;
int cur=;
for(int step=; step<(n+m)/;step++)
{
cur^=;
memset(dp[cur],,sizeof(dp[cur]));
for(int i=; i < n&&i<=step; i++)
for(int j=n-; j>=&&j>=i&&n--j<=step;j--)
{
if( ( step - i )> ( m - - ( step-( n - - j ) ) ) )continue;
int x1=i,x2=j,y1=step-i,y2=m--(step-(n--j));
if(str[x1][y1]!=str[x2][y2])continue;
dp[cur][x1][x2]=dp[cur^][x1][x2];
dp[cur][x1][x2]=( dp[cur][x1][x2] + dp[cur^][x1][x2+] )%mod;
if(x1>)
{
dp[cur][x1][x2]=( dp[cur][x1][x2] + dp[cur^][x1-][x2] )%mod;
dp[cur][x1][x2]=( dp[cur][x1][x2] + dp[cur^][x1-][x2+] )%mod;
} }
}
long long ans=;
for(int i=; i<n; i++)
ans=(ans+dp[cur][i][i])%mod;
if( (n+m)%){ for(int i=; i<n-; i++)
ans=(ans+dp[cur][i][i+])%mod;
}
printf("%I64d\n",ans);
return ;
}
D Tree Requests dfs+二分 D Pig and Palindromes -dp的更多相关文章
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- 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 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- codeforces 570 E. Pig and Palindromes (DP)
题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...
- Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coinci ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
随机推荐
- mysql数据库的备份和还原
mysql数据库的备份命令:mysqldump -u root -p 要备份的现有数据库名 > 备份后的sql文件名.sql,例如: mysqldump -u root -p heal ...
- win10 安装多个版本的jdk,如何切换
JAVA_HOME C:\software\jdk1.7.0_79 CLASSPATH .;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\jre\lib\dt.jar ...
- 【PyQt5-Qt Designer】QProgressBar() 进度条
QProgressBar() 进度条 QProgressBar简介 QProgressBar小部件提供了一个水平或垂直的进度条. 进度条用于向用户指示操作的进度,并向他们保证应用程序仍在运行. 进度条 ...
- caffe中在某一层获得迭代次数的方法以及caffe编译时报错 error: 'to_string' is not a member of 'std'解决方法
https://stackoverflow.com/questions/38369565/how-to-get-learning-rate-or-iteration-times-when-define ...
- Vue中 export default 和 export 区别
1.export与export default均可用于导出常量.函数.文件.模块等2.在一个文件或模块中,export.import可以有多个,export default仅有一个3.通过export ...
- lsof 命令
[root@localhost ~]# lsof COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init root cwd DIR , / in ...
- Windos上生成密钥,以及添加到GIT
1.下载git //进入官网下载git; https://git-scm.com/download/win 2.配置本地信息 git config --g user.name "wbiokr ...
- Stylus的使用
vue-cli项目安装使用stylus步骤:1. npm install stylus -D命令,在项目内安装stylus.(注意:命令结尾 -D 即是 --save-dev 的简写形式) 2.需要安 ...
- eclipse 优化
1.取消验证 windows–>perferences–>validation 把 除了manual 下面的全部点掉,build下只留 classpath dependency Valid ...
- 【LeetCode每天一题】Fibonacci Number(斐波那契数列)
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...