CF914E Palindromes in a Tree(点分治)
题目大意:给定一个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(点分治)的更多相关文章
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- CF914E Palindromes in a Tree(点分治)
题面 洛谷 CF 题解 题意:给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个对应字母的排列为回文. ...
- CF914E Palindromes in a Tree
$ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...
- codeforces 914E Palindromes in a Tree(点分治)
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- 【CF434E】Furukawa Nagisa's Tree 点分治
[CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...
随机推荐
- Mybatis工具Generator
转自:http://www.cuiyongzhi.com/post/36.html MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比 ...
- Tornado 高并发源码分析之一---启动一个web服务
前言: 启动一个tornado 服务器基本代码 class HomeHandler(tornado.web.RequestHandler): #创建 RequesHandler 对象,处理接收到的 h ...
- Coins and Queries(codeforce 1003D)
Polycarp has nn coins, the value of the i-th coin is aiai . It is guaranteed that all the values are ...
- Spring Cloud Feign 1(声明式服务调用Feign 简介)
Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...
- HBase 官方文档中文版
地址链接: http://abloz.com/hbase/book.html 里面包含基本的API和使用说明
- Eclipse oxygen安装中文包
help->install new software Eclipse software repository http://download.eclipse.org/technology/ba ...
- 如何上传网站程序(文件浏览器上传网页、FileZilla上传网站程序)
问题场景: 网页制作完成后,程序需上传至虚拟主机. 注意事项: Windows系统的主机请将全部网页文件直接上传到FTP根目录,即 / . Linux系统的主机请将全部网页文件直接上传到 /htdoc ...
- Docker学习笔记_安装和使用Rabbitmq
一.准备 1.宿主机OS:Win10 64bit 2.虚拟机OS:Ubuntu18.04 3.账号:docker 4.虚拟机IP:192.168.8.25 二.安装 1.搜索镜像 ...
- 3.Strings 字符串如何工作?----对缓冲区的理解。
修改Hello World程序向特定的人问好. #include <iostream> #include <string> int main() { std::string n ...
- Smarty3——内置函数
Table of Content {$var} {$append} {assign} {block} {call} {config_load} {debug} {extends} {for} {for ...