[codeforces570D]Tree Requests(dsu on the tree)
来源:互联网 发布:界面编程语言 编辑:程序博客网 时间:2024/05/21 13:54
题目:
我是超链接
题意:
一棵树,每一个点有一个字符,给出若干询问,每一次询问以某个点为根的子树中深度为x的点能否组成一个回文串。
题解:
维护树上的特定信息?dsu on the tree!
回文的话,出现次数为奇数的字符至多有一个
可以建一棵虚树,连接的是点和深度,在查询的时候,对于一个节点,可以维护ta每个深度的信息情况,再用这个虚树(链表)来查询输出
代码:
#include <cstdio>#include <iostream>#define N 500005using namespace std;int tot,nxt[N*2],point[N],v[N*2],size[N],tot1,nxt1[N*2],point1[N],v1[N*2],id[N],h[N],son[N];int cnt[N][30],Son;char st[N];bool ans[N];void addline(int x,int y){++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;}void addline1(int x,int y,int i){++tot1; nxt1[tot1]=point1[x]; point1[x]=tot1; v1[tot1]=y; id[tot1]=i;}void getson(int x,int fa){ size[x]=1; h[x]=h[fa]+1; for (int i=point[x];i;i=nxt[i]) { getson(v[i],x); if (size[v[i]]>size[son[x]]) son[x]=v[i]; size[x]+=size[v[i]]; }}void add(int x,int vv){ cnt[h[x]][st[x]-'a']+=vv; for (int i=point[x];i;i=nxt[i]) if (v[i]!=Son) add(v[i],vv); }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]; add(x,1);Son=0;//这个Son=0必须加,用来消除轻节点x的影响 for (int i=point1[x];i;i=nxt1[i]) { int odd=0; for (int j=0;j<26;j++) if (cnt[v1[i]][j]&1) odd++; if (odd>1) ans[id[i]]=0;else ans[id[i]]=1; } if (!k) add(x,-1);}int main(){ int n,m,i,j; scanf("%d%d",&n,&m); for (i=2;i<=n;i++) { int p; scanf("%d",&p); addline(p,i); } scanf("%s",st+1); getson(1,0); for (i=1;i<=m;i++) { int vv,hh; scanf("%d%d",&vv,&hh); addline1(vv,hh,i); } dfs(1,0); for (i=1;i<=m;i++) if (ans[i]) printf("Yes\n");else printf("No\n");}
阅读全文
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
- 大学生职业生涯
- 关于网线
- 小白的Construct 2(伪)技术博客
- 常用对象
- ubuntu16.04配置opencv 实现人脸识别
- [codeforces570D]Tree Requests(dsu on the tree)
- Mac环境svn的使用
- 科普 | 以链接为中心的系统:Link-based Systems
- 虚基类和虚继承
- 2.1 Spring5 快速入门
- POJ 2932 Coneology 笔记
- Linux 安装zabbix客户端
- This Activity already has an action bar supplied by the window decor
- 【LeetCode】C# 12、Integer to Roman