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. leetcode893

    class Solution { public: int numSpecialEquivGroups(vector<string>& A) { set<string> ...

  2. Python基础学习七 Excel操作

    python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...

  3. Python将两个数组合并成一个数组,多维数组变成一维数组

    1.extend方法 c1 = ["Red","Green","Blue"] c2 = ["Orange"," ...

  4. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

  5. 【HDU1573】X问题

    [题目描述] 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = ...

  6. java简单的测试方法执行了多少时间

    (1)以毫秒为单位的 long startTime = System.currentTimeMillis(); // 获取开始时间 // doThing(); // 测试的代码段 long endTi ...

  7. php_imagick超强的PHP图片处理扩展

      php_imagick是一个可以供PHP调用ImageMagick功能的PHP扩展,使用这个扩展可以使PHP具备和ImageMagick相同的功能. ImageMagick是一套功能强大.稳定而且 ...

  8. 面试题:HashMap和ConcurrentHashMap的区别,HashMap的底层源码。

    Hashmap本质是数组加链表.根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap:在hashMap的基 ...

  9. SqlServer-geography && Spatial result

    说起geography(地理)这个类型,我感觉好陌生,以前真的没有见过,今天在查询某个Address表的时候,却发现了新大陆——Spatial result(空间的结果). (1)表的结构 (2)查询 ...

  10. 关于for循环的一个小问题

    有如下程序: package com.lk.B; public class Test5 { public static void main(String[] args) { // TODO Auto- ...