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),每次询问一个子树 ...
随机推荐
- JavaScript indexOf() 方法
定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) 说明 该方法将从头 ...
- 29-2-电容触摸屏控制芯片GT911
1.接口说明 GT9 非单层多点系列(以下简称 GT9 系列) 与主机接口共有 6 PIN,分别为: VDD. GND. SCL.SDA. INT. RESET. 主控的 INT 口线需具有上升沿或下 ...
- django基础-01:软件框架,MVC框架,MVT
1. 软件框架 一个公司是由公司中的各部部门来组成的,每一个部门拥有特定的职能,部门与部门之间通过相互的配合来完成让公司运转起来. 一个软件框架是由其中各个软件模块组成的,每一个模块都有特定的功能,模 ...
- 【BP算法】
一.符号定义: al: 第l层的输出值(经过了激活函数).在DNN中是向量,在CNN中是张量. σ:激活函数的表达形式. zl: 第l层的输出值(未经过激活函数).在DNN中是向量,在CNN中是张量 ...
- Windows平台Mysql表名变小写的解决过程
由于要弄某个项目的数据库设计文档,几百张表,如果弄在word文档里面一个个添加,效率比较低. 故分别使用了Mysql WorkBench(win版)和Powerdesigner 15来做这个设计文档. ...
- abap函数返回结构体类型
1: 定义一个结构体 T-CODE se11 2: 选择 structure 3:输入相应的字段 4:激活 5:创建一个function module zfm_return_table,返回类型为 ...
- Mac charles 抓取https请求,安装证书后还是显示unknown
https://blog.csdn.net/qq_23114525/article/details/81460840 1. 配置证书 2. 设置钥匙串信任 3. 设置手机代理 端口号需要对应设置的端口 ...
- background-size的兼容性
做响应式布局的时候,如果有背景图,我们当然希望他能够全屏100%显示,这样显得页面非常的爆满,不过这又出现了一个问题,图片的尺寸多大合适呢,现在的浏览器分辨率参差不齐,对于Firefox等高级的浏览器 ...
- maven项目中添加Tomcat启动插件
在pom.xml文件中添加如下配置: <!-- 配置tomcat插件,pom.xml里配置 --> <build> <plugins> <plugin> ...
- cocos2d-x JS 富文本(为一段文本中的个别字体上颜色)
setWinText : function (levelStr1,levelStr2,levelStr3,color1,color2) { var imgRankingBG = this.contai ...