D
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 vihi. 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.

Input

The first line contains two integers nm (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 vihi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in the i-th query.

Output

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的更多相关文章

  1. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  2. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  3. 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 ...

  4. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  5. codeforces 570 E. Pig and Palindromes (DP)

    题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...

  6. 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 ...

  7. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

  8. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  9. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

随机推荐

  1. Qt5线程错误:QThread: Destroyed while thread is still running(执行runThread->exit(0))

    背景: 当前类,编写接收子线程类信号的槽函数和触发子线程类执行的信号: 新建一个子线程类,编写槽函数和信号,MyClass *m_MyClass=new MyClass(): 新建一个线程对象QThr ...

  2. 洛谷P4363 一双木棋chess [九省联考2018] 搜索+hash

    正解:记搜+hash 解题报告: 传送门! 因为看到nm范围特别小,,,所以直接考虑爆搜(bushi 先考虑爆搜之后再想优化什么的嘛QwQ 首先对这种都要最优的,就可以直接把答案设为针对某一方,然后题 ...

  3. SpringBoot-整合lombok

    添加lombok依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lom ...

  4. SQL SERVER BCP的用法

    转自:https://www.cnblogs.com/fishparadise/p/4809014.html 前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储 ...

  5. docker基本原理

    写的很不错的文章,作个存档 什么是容器 容器是 种轻量级.可移植的为应用程序提供了隔离的运行空间 .每个容器内都包含一个独享的完整用户环境,并且 个容器内的环境变动不会影响其他容器的运行环境,可以使应 ...

  6. VueI18n的应用

    .npm install vue-i18n .在 main.js 中引入 vue-i18n import VueI18n from 'vue-i18n' Vue.use(VueI18n) .在main ...

  7. 装系统w7、ubuntu、centos等系统(一)

    装w7系统准备 1.从老毛桃u盘启动盘制作工具_老毛桃u盘装系统_老毛桃pe_老毛桃官网下载装机版 2.一个正常使用的U盘,但容量大于4G,并且插入电脑保持连接 3.老毛桃装机版选择U盘启动-> ...

  8. git push 报错:missing Change-Id in commit message footer

    使用gerrit后,提交代码会出现如下截图问题: 临时解决: step1:把上面红色的那条gitidir复制下来执行下: step2:执行下面的命令会添加change_id git commit -- ...

  9. websocket协议的思考

    同过wireshark抓包,都是TCP的连接,省了好多的HTTP的头部请求 Ping Pong,TCP keep alive,双方没有数据来往的时候,通过发空白报文,侦测的报文来决定看这个链接是否还存 ...

  10. vue 使用高德地图vue-amap组件

    首先    npm install -S vue-amap 然后在 main.js import VueAMap from 'vue-amap'; //注意不要和 AMap原始名称覆盖 Vue.use ...