link

题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符)

我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文

求出对于每个点,求出经过他的神犇路径的数量

题解:

对于回文串,我们发现最多允许1个字母出现了奇数次,和%2有关

并且由于只有20个字母,说到20我就想起了二进制状压,我们对于一条链状压成20维的01向量,表示某个字符出现的次数是奇数还是偶数

说到树上静态问题我就想起淀粉质

我们考虑静态淀粉质,对于当前的树我们找出他的重心rt,然后对于每个子树DFS一下,求出某个子树到rt的路径上的所有权值(开个桶)

然后对于某个子树,在桶内减去这个子树对应的权值之后,xjb统计一下有多少个点到他路径是合法的,打个标记,然后在rt为根的树搞个树上差分就行了

然后点分下去就行了

#include <cstdio>
#include <vector>
using namespace std; int n, sz[200010], mxsz[200010], col[200010], sum, rt;
int bucket[1050000];
long long ans[200010], tmp[200010];
char str[200010];
bool vis[200010];
vector<int> out[200010]; void chkmax(int &a, int b) { if (a < b) a = b; } void getrt(int x, int fa)
{
sz[x] = 1, mxsz[x] = 0;
for (int i : out[x]) if (vis[i] == false && i != fa)
getrt(i, x), sz[x] += sz[i], chkmax(mxsz[x], sz[i]);
chkmax(mxsz[x], sum - sz[x]);
if (mxsz[x] < mxsz[rt]) rt = x;
} void getval(int x, int fa, int flag, int dis)
{
bucket[dis ^ col[x]] += flag;
for (int i : out[x]) if (vis[i] == false && fa != i) getval(i, x, flag, dis ^ col[x]);
} void qsum(int x, int fa, int dis)
{
int cur = dis ^ col[x]; tmp[x] = bucket[cur];
for (int i = 0; i < 20; i++) tmp[x] += bucket[cur ^ (1 << i)];
for (int i : out[x]) if (vis[i] == false && fa != i) qsum(i, x, cur);
} void qdis(int x, int fa)
{
for (int i : out[x]) if (vis[i] == false && fa != i) qdis(i, x), tmp[x] += tmp[i];
ans[x] += tmp[x] / ((x == rt) + 1);
} void solve(int x)
{
vis[x] = true;
for (int i : out[x]) if (vis[i] == false) getval(i, x, 1, 0);
bucket[0]++;
for (int i : out[x]) if (vis[i] == false)
getval(i, x, -1, 0), qsum(i, x, col[x]), getval(i, x, 1, 0);
bucket[0]--;
tmp[x] = bucket[col[x]];
for (int i = 0; i < 20; i++) tmp[x] += bucket[col[x] ^ (1 << i)];
qdis(x, 0);
for (int i : out[x]) if (vis[i] == false) getval(i, x, -1, 0);
for (int i : out[x]) if (vis[i] == false) rt = 0, sum = sz[i], getrt(i, 0), solve(rt);
} int main()
{
scanf("%d", &n);
for (int x, y, i = 1; i < n; i++)
scanf("%d%d", &x, &y), out[x].push_back(y), out[y].push_back(x);
scanf("%s", str + 1);
for (int i = 1; i <= n; i++) col[i] = (1 << (str[i] - 97));
mxsz[0] = sum = n, getrt(1, 0), solve(rt);
for (int i = 1; i <= n; i++) printf("%lld ", ans[i] + 1);
return 0;
}

CF914E Palindromes in a Tree(点分治)的更多相关文章

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

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

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

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

  3. CF914E Palindromes in a Tree

    $ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...

  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. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  6. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  7. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  8. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  9. 【CF434E】Furukawa Nagisa's Tree 点分治

    [CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...

随机推荐

  1. 「小程序JAVA实战」 小程序默认加载的页面和生命周期(八)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-08/ 小程序如何加载的呢?生命周期!源码:https://github.com/limingios ...

  2. 「小程序JAVA实战」微信开发者工具helloworld(三)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-03/ 第一个小程序demo的运行,首选需要去使用开发工具 开发工具下载安装 https://mp. ...

  3. Java调用Webservice(asmx)的几个例子

    Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...

  4. django dynamic model

    django model 首先对于一个习惯用django model的骚年来说,你肯定对django model自定制用的很熟悉,但突然让你用django dynamic model,也许会有很多人懵 ...

  5. Mediator模式

    [Mediator模式] 通过mediator来让各个相互通知的对象解耦.在iOS框架中就是NSNotificationCenter. 参考:http://www.jdon.com/designpat ...

  6. zookeeper集群安装的奇怪现象

    zookeeper:配置的集群信息是domain:端口2888:端口3888:  domain为内网静态ip:每次启动都不能相互连接报错误: [myid:3] - WARN  [WorkerSende ...

  7. 基于size的优化

    ----------------------siwuxie095                                 基于 size 的优化         在 union( p , q ...

  8. POI技术

    1.excel左上角有绿色小图标说明单元格格式不匹配 2.模板中设置自动计算没效果,需要加上sheet.setForceFormulaRecalculation(true); FileInputStr ...

  9. 关于wamp中升级PHP+Apache 的问题

    首先个人不建议wamp中升级php版本,如果你不信可以试一试,当你php升级后发想,奥,Apache版本不匹配,然后又去升级Apache,结果搞了半天,弄出来了就好,要是没出来,可能你会气死(好吧,气 ...

  10. 丢弃昂贵的Detours Professional 3.0,使用免费强大的EasyHook

    我们要先看看微软官方的著名HOOK库: Detours Professional 3.0 售价:US$9,999.95 功能列表: Detours 3.0 includes the following ...