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 ...
随机推荐
- shellSyntax.md
shell #! /bin/bash 头文件 echo -e 若字符串出现特殊字符,则特别处理 -n 不要在最后自动换行 ./filename.sh sh filename.sh r ...
- 给编译出的程序添加图标(exe的图标)
安装依赖: go get github.com/akavel/rsrc 在对应程序的源码路径下创建manifest文件,图标也要放进去(xxx.ico), 命名:main.exe.manifest : ...
- LeetCode557 反转字符串中的单词 III
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: &q ...
- nodejs中使用worker_threads来创建新的线程
目录 简介 worker_threads isMainThread MessageChannel parentPort和MessagePort markAsUntransferable SHARE_E ...
- bean与map之间的转化
import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; impor ...
- java进阶(33)--IO流
一.IO流概念:1.基本概念2.IO流分类3.java.io流的四大家族4.流的close和flush方法5.java.id下常用的16个流 二.FileInputStream字节输入流1.FileI ...
- SEO大杀器rendertron安装
前段时间做SEO的优化,使用的是GoogleChrome/rendertron,发现这个安装部署的时候还是会有一些要注意的地方,做个记录 为什么要使用rendertron 目前很多网站都是使用 vue ...
- 前端中的script标签
script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...
- 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。
1 import org.apache.hadoop.conf.Configuration; 2 import org.apache.hadoop.fs.*; 3 import org.apache. ...
- smtplib.py
def _print_debug(self, *args): if self.debuglevel > 1: print(datetime.datetime.now().time(), *arg ...