$ \color{#0066ff}{ 题目描述 }$

给你一颗 n 个顶点的树(连通无环图)。顶点从 1 到 n 编号,并且每个顶点对应一个在‘a’到‘t’的字母。 树上的一条路径是回文是指至少有一个对应字母的排列为回文。 对于每个顶点,输出通过它的回文路径的数量。 注意:从u到v的路径与从v到u的路径视为相同,只计数一次。

\(\color{#0066ff}{输入格式}\)

第一行包含一个整数n(2<=n<=2*10^5)。 接下来的 n-1 行,每行包含两个整数u和v(1<=u,v<=n,u≠v)表示一条连接u和v的边。保证给出的图是一棵树。 再下一行包含一个n个字符的字符串,第i个字符对应第i个顶点。

\(\color{#0066ff}{输出格式}\)

输出一行,包含n个整数,第i个数表示经过顶点i的回文路径数量。

\(\color{#0066ff}{输入样例}\)

5
1 2
2 3
3 4
3 5
abcbb7
6 2
4 3
3 7
5 2
7 2
1 4
afefdfs

\(\color{#0066ff}{输出样例}\)

1 3 4 3 3 1 4 1 1 2 4 2 

\(\color{#0066ff}{数据范围与提示}\)

In the first sample case, the following paths are palindromic:

\(2−3−4\)

\(2−3−5\)

\(4−3−5\)

Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic:

\(1−2−3\)

\(1−2−3−4\)

\(1−2−3−5\)

4000ms / 256MB

\(\color{#0066ff}{题解}\)

看到时限还有求树链的个数,肯定是点分治跑不了了

对于当前点,把它到所有点的树链的状态搜出来存入桶中

然后开始统计经过他的链对所有点的贡献

对于每棵子树,先dfs一遍消去子树对桶的贡献(统计的是子树间的链)

然后开始统计贡献

假如当前点是u,对于任意子树中的某一点v,如果v的子树中某点k,\(u-k\)的链合法,那么实际上v也要+1,所以说一个点的贡献不仅仅是自己到u和其它子树到u的贡献,还有自己的孩子到u和其它子树到u的贡献,也就是说,贡献还要加上子树的贡献和!

对于u自己的贡献还有单链的贡献,可以单独考虑

统计完贡献, 恢复这个子树对桶的贡献,继续下一子树

全弄完,再整个dfs一遍清空桶,分治子节点

#include<bits/stdc++.h>
#define LL long long
LL in() {char ch; LL x = 0, f = 1;while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));return x * f;
}
const int maxn = 2e5 + 10;
struct node {int to;node *nxt;node(int to = 0, node *nxt = NULL): to(to), nxt(nxt) {}
};
node *head[maxn];
std::vector<int> v, vv;
LL ans[maxn];
int val[maxn], sum, root;
int t[1 << 21], f[maxn], siz[maxn];
bool vis[maxn];
int n;
void add(int from, int to) {head[from] = new node(to, head[from]);
}
int getch() {char ch;while(!isalpha(ch = getchar()));return ch - 'a';
}
void getroot(int x, int fa) {siz[x] = 1, f[x] = 0;for(node *i = head[x]; i; i = i->nxt) {if(i->to == fa || vis[i->to]) continue;getroot(i->to, x);siz[x] += siz[i->to];f[x] = std::max(f[x], siz[i->to]);}f[x] = std::max(f[x], sum - siz[x]);if(f[x] < f[root]) root = x;
}
void dfs(int x, int fa, int zt) {v.push_back(zt);vv.push_back(zt);for(node *i = head[x]; i; i = i->nxt) {if(i->to == fa) continue;dfs(i->to, x, zt ^ (1 << val[i->to]));}
}
void build(int x, int fa, int zt, int k) {t[zt ^ (1 << val[x])] += k;for(node *i = head[x]; i; i = i->nxt) {if(i->to == fa || vis[i->to]) continue;build(i->to, x, zt ^ (1 << val[x]), k);}
}
int getans(int x, int fa, int zt) {int tot = t[zt ^ (1 << val[x])];for(int k = 0; k <= 19; k++) tot += t[zt ^ (1 << val[x]) ^ (1 << k)];for(node *i = head[x]; i; i = i->nxt) {if(i->to == fa || vis[i->to]) continue;tot += getans(i->to, x, zt ^ (1 << val[x]));}ans[x] += tot;return tot;
}
void calc(int x) {build(x, 0, 0, 1);int tot = t[0];for(int i = 0; i <= 19; i++) tot += t[1 << i];for(node *i = head[x]; i; i = i->nxt) {if(vis[i->to]) continue;build(i->to, x, 1 << val[x], -1);tot += getans(i->to, x, 0);build(i->to, x, 1 << val[x], 1);}ans[x] += tot >> 1;build(x, 0, 0, -1);
}
void work(int x) {vis[x] = true;calc(x);for(node *i = head[x]; i; i = i->nxt) {if(vis[i->to]) continue;sum = siz[i->to], root = 0;getroot(i->to, x);work(root);}
}int main() {n = in();int x, y;for(int i = 1; i < n; i++) x = in(), y = in(), add(x, y), add(y, x);for(int i = 1; i <= n; i++) val[i] = getch();root = 0, sum = f[0] = n;getroot(1, 0);work(root);for(int i = 1; i <= n; i++) printf("%lld%c", ans[i] + 1, i == n? '\n' : ' ');return 0;
}

转载于:https://www.cnblogs.com/olinr/p/10484384.html

CF914E Palindromes in a Tree相关推荐

  1. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  2. 有趣题目和认知合集(持续更新)

    写写对一些算法的理解,挂几个有意思的题,可能也会挂几个板子题 算法理解偏向于能懂即可,没有严格的证明 快乐几何 [1.2]Volatile Kite 点到直线 快乐搜与暴力 [2.4]Short Co ...

  3. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link:  http://acm.timus.ru/problem.aspx?space=1&num=1 ...

  4. Codeforces Round #315 (Div. 1) A. Primes or Palindromes? 暴力

    A. Primes or Palindromes? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=326 ...

  5. Palindromic Tree——回文树【处理一类回文串问题的强力工具】

    今天我们来学习一个神奇的数据结构:Palindromic Tree.中译过来就是--回文树. 那么这个回文树有何功能? 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~ ...

  6. 回文树介绍(Palindromic Tree)

    简介 回文树是由Mikhail Rubinchik大神发明的,在Petrozavodsk Summer Camp 2014上首次提出来,是一个很新的数据结构,目前相关资料比较少. 顾名思义,回文树是一 ...

  7. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

  9. Python---哈夫曼树---Huffman Tree

    今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...

最新文章

  1. Wordpress 加入html等文件
  2. 安卓 存储---SharedPreferences
  3. Java多线程(七)之同步器基础:AQS框架深入分析
  4. Chrome 浏览器扩展 - Night Eye
  5. SpringBoot之基础
  6. js中for循环调用回调函数,一直循环最后一个
  7. html 拖放实现拼图游戏,Canvas drag 实现拖拽拼图小游戏
  8. 如何在Azure上创建和部署云服务
  9. Webpack 中 css import 使用 alias 相对路径
  10. 由淘宝,京东,凡客站点的多条件分页查询细节想到的
  11. [论文]基于强化学习的控制输入非线性水下机器人自适应神经网络控制
  12. 计算机二级office学习之Excel操作题考点整理
  13. Qt之QTcpSocket 跨线程连续发送大数据
  14. 用matlab算特征值,用Matlab用计算特征值和特征向量
  15. 普渡大学 计算机金融,普渡大学金融数学专业排名2019年
  16. 笔记本电脑WIN10开热点手机无法连接的问题
  17. 洲际酒店集团加速布局西部世界,全力打造高品质中国山地旅游
  18. element 刷新
  19. vue push html,html5 - Vue 2.0 javaScript 数组循环push json 对象问题
  20. 友盟推送服务器配置文档,友盟使用指南

热门文章

  1. 使用SQL命令行更改数据库字段类型
  2. 逆序枚举时常犯的一个错误
  3. ubuntu nginx php问题研究
  4. java分层窗格_java 简洁的分层实现
  5. mysql tuning primer_mysql检测工具tuning-primer.sh
  6. spark基础之checkpoint机制
  7. (13)ISE14.7bit文件生成mcs文件(FPGA不积跬步101)
  8. (67)Verilog HDL模块条件例化
  9. (17)Verilog HDL结构:always语句
  10. s7-300 400plc应用技术_西门子S7300/400顺序功能图设计教程,看完豁然开朗!