【题目】E. Palindromes in a Tree

【题意】给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数。n<=2*10^5。

【算法】点分治

【题解】状压20位的二进制表示一条路径的字符状态,点分治过程中维护扫描过的路径只须维护状态桶数组,t[i]表示前面状态为i的路径条数。

合并:考虑当前状态为j,要使合并的状态满足条件即i^j=1<<k(0<=k<20)或i^j=0,移项得i=j^(1<<k)或i=j,所以路径数是Σ t [ j^(1<<k) ]+t[j]。

统计每个点:对于当前要处理的重心x,先遍历所有子树得到整个t[]数组,然后对每个子树先删除其在桶里的状态,然后扫一遍贡献子树中每个点,最后将子树的状态加回桶中。

这样可以做到每条路径都贡献到每个点,要特殊处理重心的贡献。

复杂度O(n log n)。

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<algorithm>
  4. #define ll long long
  5. using namespace std;
  6. const int maxn=,maxN=;
  7. int tot,first[maxn],sz[maxn],vis[maxn],sum,root,a[maxn],u,v,n;
  8. ll ans[maxn],t[maxN];
  9. struct edge{int v,from;}e[maxn*];
  10.  
  11. void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
  12. void getroot(int x,int fa){
  13. sz[x]=;
  14. bool ok=;
  15. for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v]){
  16. getroot(e[i].v,x);
  17. sz[x]+=sz[e[i].v];
  18. if(sz[e[i].v]>sum/)ok=;
  19. }
  20. if(ok&&sz[x]>=sum/)root=x;
  21. }
  22. void dfs(int x,int fa,int p,int s){
  23. t[s^=(<<a[x])]+=p;
  24. for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v])dfs(e[i].v,x,p,s);
  25. }
  26. ll calc(int x,int fa,int s){
  27. s^=(<<a[x]);ll num=t[s];
  28. for(int i=;i<;i++)num+=t[s^(<<i)];
  29. for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v])num+=calc(e[i].v,x,s);
  30. ans[x]+=num;
  31. return num;
  32. }
  33. void solve(int x,int s){
  34. vis[x]=;
  35. dfs(x,,,);
  36. ll num=t[];
  37. for(int i=;i<;i++)num+=t[<<i];
  38. for(int i=first[x];i;i=e[i].from)if(!vis[e[i].v]){
  39. dfs(e[i].v,x,-,<<a[x]);
  40. num+=calc(e[i].v,x,);
  41. dfs(e[i].v,x,,<<a[x]);
  42. }
  43. ans[x]+=num/;
  44. dfs(x,,-,);
  45. for(int i=first[x];i;i=e[i].from)if(!vis[e[i].v]){
  46. if(sz[e[i].v]>sz[x])sum=s-sz[x];else sum=sz[e[i].v];
  47. getroot(e[i].v,x);
  48. solve(root,sum);
  49. }
  50. }
  51. char s[maxn];
  52. int main(){
  53. scanf("%d",&n);
  54. for(int i=;i<n;i++){
  55. scanf("%d%d",&u,&v);
  56. insert(u,v);insert(v,u);
  57. }
  58. scanf("%s",s+);
  59. for(int i=;i<=n;i++)a[i]=s[i]-'a';
  60. sum=n;
  61. getroot(,);
  62. solve(root,sum);
  63. for(int i=;i<=n;i++)printf("%lld ",ans[i]+);
  64. return ;
  65. }

【CodeForces】914 E. Palindromes in a Tree 点分治的更多相关文章

  1. Codeforces 1039D You Are Given a Tree [根号分治,整体二分,贪心]

    洛谷 Codeforces 根号分治真是妙啊. 思路 考虑对于单独的一个\(k\)如何计算答案. 与"赛道修建"非常相似,但那题要求边,这题要求点,所以更加简单. 在每一个点贪心地 ...

  2. CF914E Palindromes in a Tree(点分治)

    link 题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符) 我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文 求出对于每个点,求出经过他的神犇路径的数量 题解: ...

  3. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

  4. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. CodeForces 1251B --- Binary Palindromes

    [CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...

  7. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  8. codeforces 914E Palindromes in a Tree(点分治)

    You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...

  9. Palindromes in a Tree CodeForces - 914E

    https://vjudge.net/problem/CodeForces-914E 点分就没一道不卡常的? 卡常记录: 1.把不知道为什么设的(unordered_map)s换成了(int[])s ...

随机推荐

  1. 复利计算C语言转java的相关代码

    static void principal()// 计算本金 { int N, m; double i, F, P; System.out.printf("复利终值:"); F = ...

  2. 用友 SAP 金蝶 季报

    用友 2018Q3季报 营收:.42亿 营收收入同比增长:42.36% 净利润:.35万 净利润同比增长率:113.83% 销售毛利率:66.88% 销售净利率:19.29% 用友2017财年年报 营 ...

  3. 理解promise 01

    原文地址: http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html 用Javascript的小伙伴们,是时候承认了,关于 ...

  4. urllib2 request 模拟伪装浏览器

    直接上代码吧 # -*- coding:utf-8 -*- import urllib2 import random url = "http://www.baidu.com/" # ...

  5. 一张图看懂css的position里的relative和absolute的区别

    position有以下属性:static.inherit.fixed.absolute.relative前三个好理解好区分:static:是默认状态,没有定位,元素出现在正常的流中(忽略 top, b ...

  6. npm 安装 不快的解决办法

    npm config list  查看配置 npm config set prefix “c:\dev\nvm\npm”(配置用npm下载包时全局安装的包路径) npm install npm -g ...

  7. dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象

    dom变成jquery对象   先获取dom对象 然后通过$()转换成jquery对象

  8. 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp

    题目描述 给出一个有 $2^n$ 个叶子节点的完全二叉树.每个叶子节点可以选择黑白两种颜色. 对于每个非叶子节点左子树中的叶子节点 $i$ 和右子树中的叶子节点 $j$ :如果 $i$ 和 $j$ 的 ...

  9. IOS8模糊毛玻璃的效果UIVisualEffectView

    UIVisualEffectView实现两种模糊效果:UIBlurEffect 和 UIVibrancyEffect 两者都是继承自UIView,前者放在任意的View里边都能对下册的视图渲染出模糊效 ...

  10. [学习笔记]搜索——模拟与dp的结合

    搜索: 一种基础的算法. 考察常见于NOIP 但是高级的搜索算法可能还会在省选出现. 50%以上的暴力都可以用搜索直接枚举来写. 但是,当数据规模不是很大的时候,搜索也可能成为正解. (比如剪枝PK状 ...