【题目】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. nginx 几个常用的标准模块介绍

    ngx_http_ssl_module(https) 1:指明是否启用的虚拟主机的ssl功能 ssl on | off; 2:指明虚拟主机使用的证书文件 ssl_certificate /usr/lo ...

  2. 软件工程个人作业3——集大通APP案例分析

    第一部分:调研, 评测 1.第一次上手体验 主要界面截图: 感受: 1.界面不美观: 2.特色功能展现模块不突出,以上截图为打开APP所看到的界面展示,但是这些功能都不是该APP的特色功能,显得有些累 ...

  3. Ubuntu 下升级 php

    起因: 在现有的 Apache + PHP 环境下,增加一个 PHP Extension 扩展时,遇到错误: Unable to initialize moduleModule compiled wi ...

  4. jquery中on绑定click事件在苹果手机失效问题解决(巨坑啊)

    描述:用一个div写一个按钮,并给这个按钮添加一个点击事件,在安卓机器上一切正常,但是在苹果机型上会出现点击事件失效. <!DOCTYPE html> <html lang=&quo ...

  5. Maven 私服安装和启动

    在安装私服的时候容易碰到的两个问题,一个是安装时拒绝访问,另一个是安装完成后服务无法启动: 拒绝访问问题: 原因:没有以管理员身份运行 cmd 解决办法: 如果是 win7 的话,可以直接在 [运行- ...

  6. Linux下启用MySQL慢查询

    MySQL在linux系统中的配置文件一般是my.cnf找到[mysqld]下面加上log-slow-queries=/data/mysqldata/slowquery.loglong_query_t ...

  7. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  8. bzoj1390 [CEOI2008] Fence

    题意 给出n个白点和m个黑点.现在你需要选择一些白点把黑点圈起来.每有一个黑点不能被选出的白点组成的凸包包含就需要付出111的代价,每选出一个白点就需要付出20的代价.要求最小化代价之和 n,m< ...

  9. Unbuntu+nginx+mysql+php

    1/准备 sudo su --切换到root 2/nginx安装 apt-get update apt-get install nginx 3/mysql 安装 apt-get install mys ...

  10. P1107 [BJWC2008]雷涛的小猫

    题目描述 雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学生宿舍管理条例的).在他的照顾下,小猫很快恢复了健康,并且愈发的活泼可爱了. 可是有一天,雷涛下课回 ...