[Codeforces570D]Tree Requests(dsu on the tree)
来源:互联网 发布:厄米特矩阵的逆也是 编辑:程序博客网 时间:2024/05/29 09:14
题目描述
传送门
题意:一棵树,每一个点有一个字符,给出若干询问,每一次询问以某个点为根的子树中深度为x的点能否组成一个回文串。
题解
用dsu on the tree搞一下。。
如果可以组成一个回文串的话出现次数为奇数的字符至多有一个
记录一下深度为i的点中字符为j的个数,然后对于每一个点的询问搞一个链表查询就可以了
时间复杂度
代码
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define N 500005int n,m,Son;char s[N];int tot,point[N],nxt[N],v[N];int cq,pq[N],nxtq[N],vq[N],idq[N];int size[N],h[N],son[N],cnt[N][30];bool ans[N];void add(int x,int y){ ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;}void addq(int x,int y,int z){ ++cq; nxtq[cq]=pq[x]; pq[x]=cq; vq[cq]=y; idq[cq]=z;}void getson(int x,int dep){ size[x]=1;h[x]=dep; for (int i=point[x];i;i=nxt[i]) { getson(v[i],dep+1); size[x]+=size[v[i]]; if (size[v[i]]>size[son[x]]) son[x]=v[i]; }}void calc(int x,int opt){ cnt[h[x]][s[x]-'a']+=opt; for (int i=point[x];i;i=nxt[i]) if (v[i]!=Son) calc(v[i],opt);}void dfs(int x,int k){ for (int i=point[x];i;i=nxt[i]) if (v[i]!=son[x]) dfs(v[i],0); if (son[x]) dfs(son[x],1),Son=son[x]; calc(x,1);Son=0; for (int i=pq[x];i;i=nxtq[i]) { int H=vq[i],odd=0; for (int j=0;j<26;++j) if (cnt[H][j]&1) ++odd; if (odd<=1) ans[idq[i]]=1; } if (!k) calc(x,-1);}int main(){ scanf("%d%d",&n,&m); for (int i=2;i<=n;++i) { int fa;scanf("%d",&fa); add(fa,i); } scanf("%s",s+1); for (int i=1;i<=m;++i) { int x,y;scanf("%d%d",&x,&y); addq(x,y,i); } getson(1,1); dfs(1,0); for (int i=1;i<=m;++i) if (ans[i]) puts("Yes"); else puts("No");}
0 0
- [Codeforces570D]Tree Requests(dsu on the tree)
- [codeforces570D]Tree Requests(dsu on the tree)
- codeforces 570 D. Tree Requests (dsu on the tree)
- cf570D. Tree Requests(dsu on tree)
- CodeForces570D - Tree Requests 【dfs序】
- [Codeforces375D]Tree and Queries(dsu on the tree+bit)
- [codeforces375D]Tree and Queries(dsu on the tree+bit)
- codeforces 600 E. Lomsat gelral (dsu on the tree)
- codeforces 208 E. Blood Cousins (dsu on the tree)
- [Codeforces600E]Lomsat gelral(dsu on the tree)
- [Codeforces208E]Blood Cousins(dsu on the tree+倍增)
- [Codeforces246E]Blood Cousins Return(dsu on the tree+set)
- [codeforces600E]Lomsat gelral(dsu on the tree+讲解)
- [codeforces208E]Blood Cousins(dsu on the tree+倍增)
- [codeforces246E]Blood Cousins Return(dsu on the tree+STL)
- dsu on the tree 学习笔记
- [trick]dsu on tree
- dsu on tree
- 使用matplotlib画简单的等高线图
- GYM 100090 I.Retakes(贪心+优先队列)
- NGUI与UGUI
- javascript闭包操作
- 第五届蓝桥杯【省赛试题1】啤酒和饮料
- [Codeforces570D]Tree Requests(dsu on the tree)
- kmalloc分配大小的限制
- Fiddler2中文乱码问题
- swfit 的 get 和 set 方法, willSet 和 didSet
- Android系统源码下载搭建步骤
- Spark机器学习
- MySQL InnoDB 高性能应用的一些知识要点
- 对于系统频繁发送的广播需要动态注册
- 申请邓白氏编码