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),每次询问一个子树 ...
随机推荐
- java之获取资源文件
背景介绍 在java程序中有时我们需要加载项目中的某些资源文件(如:config.properties之类),以便获取里面的值,这样可以避免某些需要经常修改的数据硬编码入业务程序中 实现方式 实现这种 ...
- sharepoint webapp 部署注意点
只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta ...
- g++编译多个文件
注意:头文件不用去指定,其是由#include命令进行管理的,只需要编译cpp文件就可以了: 举例: 有以下三个文件: a.h a.cpp main.cpp 那么编译可以有以下两种方式: 1.分开编译 ...
- gitlab:开发+测试+发布的全流程图
- 萌新接触前端的第一课——HTML
HTML web服务本质(好吧这个先不用知道也可以) import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOC ...
- MySQL 8.0.11 报错[ERROR] [MY-011087] Different lower_case_table_names settings for server ('1')
--报错信息: 2018-06-07T19:52:26.943083+08:00 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld ...
- Python3学习之路~5.4 os模块
用于提供系统级别的操作 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shel ...
- 运维自动化ansible基础
云计算三种服务架构 IAAS: 不提供OS 只购买硬件(网络,存储,计算) PAAS: 提供硬件和OS和开发和运行环境 只需要开发应用软件 SAAS: 提供 硬件 os 软件 相当于直接购买软 ...
- PXE安装操作系统
TFTP服务 用PXE安装操作系统依赖于DHCP服务和TFTP服务 网卡一般都内置的TFTP客户端的程序 systemctl enable tftp systemctl enable dhc ...
- (转)redis分布式锁-SETNX实现
Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...