Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)
题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串。
思路:暴力dsu找一边 简直就是神技!
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const int N=500007;
const ll mod=1e9+7;
struct node{
int h,id;
bool f;
friend bool operator < (node a,node b){
return a.id<b.id;
}
};
vector<int> G[N];
char val[N];
int dp[N],son[N],cnt[N][26];
int odd[N];
int de[N];
int po=0;
vector<node> h[N];
node ans[N];
void dfs(int u,int fa,int deep){
dp[u]=1;
de[u]=deep;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u,deep+1);
dp[u]+=dp[v];
if(dp[v]>dp[son[u]]) son[u]=v; //树剖
}
}
void add(int u,int fa,int w,int deep){
cnt[deep][val[u]-'a']+=w;
if(cnt[deep][val[u]-'a']&1) odd[deep]++;
else odd[deep]--;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa||v==po) continue;
add(v,u,w,deep+1);
}
}
int num=0;
void dfss(int u,int fa,int opt,int deep){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
if(v!=son[u]) dfss(v,u,0,deep+1);
}
if(son[u]) dfss(son[u],u,1,deep+1),po=son[u];
add(u,fa,1,deep);
po=0;
for(int i=0;i<h[u].size();i++){
node tmp=h[u][i];
if(odd[tmp.h]>=2) tmp.f=0;
ans[++num]=tmp;
}
if(!opt) add(u,fa,-1,deep);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m; cin>>n>>m;
for(int i=2;i<=n;i++){
int fa; cin>>fa;
G[fa].push_back(i);
}
for(int i=1;i<=n;i++){
cin>>val[i];
}
for(int i=1;i<=m;i++){
int vi,hi; cin>>vi>>hi;
node tmp; tmp.h=hi; tmp.f=1; tmp.id=i;
h[vi].push_back(tmp);
}
dfs(1,0,1);
dfss(1,0,1,1);
sort(ans+1,ans+1+m);
for(int i=1;i<=num;i++){
if(ans[i].f) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)的更多相关文章
- 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 Round #316 (Div. 2) D Tree Requests
官方题解是离线询问,dfs树形转线性,然后二分找区间. 还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案. 代码是学 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #316 (Div. 2)
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- 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 #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
随机推荐
- LeetCode 二分查找模板 III
模板 #3: int binarySearch(vector<int>& nums, int target){ if (nums.size() == 0) return -1; i ...
- 【Java并发集合】ConcurrentHashMap源码解析基于JDK1.8
concurrentHashMap(基于jdk1.8) 类注释 所有的操作都是线程安全的,我们在使用时无需进行加锁. 多个线程同时进行put.remove等操作时并不会阻塞,可以同时进行,而HashT ...
- nginx日志详细说明
Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的. 访问日志 访问日志主要记录 ...
- 【Python】PDF转WORD
注意,下文中的PDF文档是纯文字格式,而且非扫描版的PDF文件. 如果是扫描版或者带有图片的.可能转起来会出现排版异常并且图片无法保存到.doc文件中. 正文开始: 需要安装依赖包 pdfminer3 ...
- 【Linux】awk想打印制定列以后的所有列
今天偶然研究awk,有一个文件,文件内容是全篇的1 2 3 4 5 6 7 8 9 0 现在想打印除了第一列意外的所有列 文件内容: [root@localhost ~]# cat test.txt ...
- 【Oracle】10.2.0.1升级到10.2.0.5
升级数据库到10.2.0.5 因是测试环境,不需要备份:如是生产系统,建议进行全备份后再进行升级操作,预防数据丢失造成不必要的影响. 步骤: 上传并解压补丁,安装前准备,安装补丁,预升级检查, ...
- window安装nvm
先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...
- luogu P1453 城市环路
题目描述 整个城市可以看做一个N个点,N条边的单圈图(保证图连通),唯一的环便是绕城的环路.保证环上任意两点有且只有2条路径互通.图中的其它部分皆隶属城市郊区. 现在,有一位名叫Jim的同学想在B市开 ...
- 提取当前文件夹下的所有文件名.bat(Windows批处理文件)
@echo off dir /s/b *.* > 文件名.txt exit
- mysqldump 内存消耗
MySQL :: MySQL 8.0 Reference Manual :: 4.5.4 mysqldump - A Database Backup Program https://dev.mysql ...