$ \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
abcbb 7
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;
}

CF914E Palindromes in a Tree的更多相关文章

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

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

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

    题面 洛谷 CF 题解 题意:给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个对应字母的排列为回文. ...

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

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

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

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

  5. Palindromes in a Tree CodeForces - 914E

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

  6. Codeforces914E. Palindromes in a Tree

    n<=100000的树,每个点上有个字母a-t之一,问有多少这样的链经过每个点:它的某一个排列的字母串起来是回文的. 就是有最多一个字母是奇数个啦..这样点分算一波即可..细节较多详见代码 #i ...

  7. ghj1222被坑记录[不持续更新]

    考试注意事项:link1 link2 (密码:wangle) 调不出来bug,可以先透彻一会儿或者是上个厕所或者坐一会别的题(间隔至少20min),然后通读代码 -1. 考试先读题,读题之后搞出一个做 ...

  8. CodeChef Tree Palindromes

    Tree Palindromes Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin cha ...

  9. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

随机推荐

  1. IE回车的一个怪异行为

    IE中在input中回车相当于提交form,会从dom中找最近的button标签触发click事件 <!DOCTYPE html> <html> <head> &l ...

  2. 最小的VIM操作指南

    最小VIM操作指南 vim的操作命令非常多,为了能开始工作,必须学会一个最小的vim操作集合,这里做个总结. 1.插入.追加.插入新行 i:在当前光标所在字符的前面插入,当前字符及其后面的字符后撤 a ...

  3. 201671010140. 2016-2017-2 《Java程序设计》java学习第二周

                                 学习第二周(Java基本程序设计结构)      这一周,着重学习了Java的简单程序设计实现及运行,通过自己操作,发现Java的程序语法大面 ...

  4. 动画系统II

    [动画系统II] 1.动画混合(animation blending)是把某个时间点的两个或更多的输入姿势结合,产生骨骼的输出姿势.例如,通过混合负伤的及无负伤的步行动画,我们可以生成二者之间不同负伤 ...

  5. Gym 101128 B Black Vienna

    题意 有A-Z 26张牌,现在从中抽出3张牌,并把剩下的23张牌分给选手1和2,现在有n次询问,每次询问一个选手是否有某两张牌,和选手的回答.回答说自己有这两张牌中的几张,问拿出的三张牌有多少种方案能 ...

  6. 【总结整理】KANO 模型

    c 基本(必备)型需求——Must-beQuality/ Basic Quality. 期望(意愿)型需求——One-dimensional Quality/ Performance Quality. ...

  7. the install of mysql in Linux System

    一.下载MySql 浏览器打开 https://www.mysql.com/downloads/mysql/#downloads 下载 我下载的版本是Red Hat 5 版本的 https://www ...

  8. 【转】LVS负载均衡之session解决方案 持久连接

    原文地址:http://minux.blog.51cto.com/8994862/1744761 1. 持久连接是什么? 1.1 在LVS中,持久连接是为了用来保证当来自同一个用户的请求时能够定位到同 ...

  9. linux ssh使用深度解析(key登录详解)

    linux ssh使用深度解析(key登录详解) SSH全称Secure SHell,顾名思义就是非常安全的shell的意思,SSH协议是IETF(Internet Engineering Task ...

  10. Nginx 模块开发

    Nginx 模块概述 Nginx 模块有三种角色: 处理请求并产生输出的 Handler 模块 : 处理由  Handler  产生的输出的 Filter (滤波器)模块: 当出现多个后台 服务器时, ...