【CodeForces】914 E. Palindromes in a Tree 点分治
【题意】给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数。n<=2*10^5。
【算法】点分治
【题解】状压20位的二进制表示一条路径的字符状态,点分治过程中维护扫描过的路径只须维护状态桶数组,t[i]表示前面状态为i的路径条数。
合并:考虑当前状态为j,要使合并的状态满足条件即i^j=1<<k(0<=k<20)或i^j=0,移项得i=j^(1<<k)或i=j,所以路径数是Σ t [ j^(1<<k) ]+t[j]。
统计每个点:对于当前要处理的重心x,先遍历所有子树得到整个t[]数组,然后对每个子树先删除其在桶里的状态,然后扫一遍贡献子树中每个点,最后将子树的状态加回桶中。
这样可以做到每条路径都贡献到每个点,要特殊处理重心的贡献。
复杂度O(n log n)。
- #include<cstdio>
- #include<cctype>
- #include<algorithm>
- #define ll long long
- using namespace std;
- const int maxn=,maxN=;
- int tot,first[maxn],sz[maxn],vis[maxn],sum,root,a[maxn],u,v,n;
- ll ans[maxn],t[maxN];
- struct edge{int v,from;}e[maxn*];
- void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
- void getroot(int x,int fa){
- sz[x]=;
- bool ok=;
- for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v]){
- getroot(e[i].v,x);
- sz[x]+=sz[e[i].v];
- if(sz[e[i].v]>sum/)ok=;
- }
- if(ok&&sz[x]>=sum/)root=x;
- }
- void dfs(int x,int fa,int p,int s){
- t[s^=(<<a[x])]+=p;
- for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v])dfs(e[i].v,x,p,s);
- }
- ll calc(int x,int fa,int s){
- s^=(<<a[x]);ll num=t[s];
- for(int i=;i<;i++)num+=t[s^(<<i)];
- for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa&&!vis[e[i].v])num+=calc(e[i].v,x,s);
- ans[x]+=num;
- return num;
- }
- void solve(int x,int s){
- vis[x]=;
- dfs(x,,,);
- ll num=t[];
- for(int i=;i<;i++)num+=t[<<i];
- for(int i=first[x];i;i=e[i].from)if(!vis[e[i].v]){
- dfs(e[i].v,x,-,<<a[x]);
- num+=calc(e[i].v,x,);
- dfs(e[i].v,x,,<<a[x]);
- }
- ans[x]+=num/;
- dfs(x,,-,);
- for(int i=first[x];i;i=e[i].from)if(!vis[e[i].v]){
- if(sz[e[i].v]>sz[x])sum=s-sz[x];else sum=sz[e[i].v];
- getroot(e[i].v,x);
- solve(root,sum);
- }
- }
- char s[maxn];
- int main(){
- scanf("%d",&n);
- for(int i=;i<n;i++){
- scanf("%d%d",&u,&v);
- insert(u,v);insert(v,u);
- }
- scanf("%s",s+);
- for(int i=;i<=n;i++)a[i]=s[i]-'a';
- sum=n;
- getroot(,);
- solve(root,sum);
- for(int i=;i<=n;i++)printf("%lld ",ans[i]+);
- return ;
- }
【CodeForces】914 E. Palindromes in a Tree 点分治的更多相关文章
- Codeforces 1039D You Are Given a Tree [根号分治,整体二分,贪心]
洛谷 Codeforces 根号分治真是妙啊. 思路 考虑对于单独的一个\(k\)如何计算答案. 与"赛道修建"非常相似,但那题要求边,这题要求点,所以更加简单. 在每一个点贪心地 ...
- CF914E Palindromes in a Tree(点分治)
link 题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符) 我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文 求出对于每个点,求出经过他的神犇路径的数量 题解: ...
- codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- CodeForces 1251B --- Binary Palindromes
[CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- codeforces 914E Palindromes in a Tree(点分治)
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...
- Palindromes in a Tree CodeForces - 914E
https://vjudge.net/problem/CodeForces-914E 点分就没一道不卡常的? 卡常记录: 1.把不知道为什么设的(unordered_map)s换成了(int[])s ...
随机推荐
- 复利计算C语言转java的相关代码
static void principal()// 计算本金 { int N, m; double i, F, P; System.out.printf("复利终值:"); F = ...
- 用友 SAP 金蝶 季报
用友 2018Q3季报 营收:.42亿 营收收入同比增长:42.36% 净利润:.35万 净利润同比增长率:113.83% 销售毛利率:66.88% 销售净利率:19.29% 用友2017财年年报 营 ...
- 理解promise 01
原文地址: http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html 用Javascript的小伙伴们,是时候承认了,关于 ...
- urllib2 request 模拟伪装浏览器
直接上代码吧 # -*- coding:utf-8 -*- import urllib2 import random url = "http://www.baidu.com/" # ...
- 一张图看懂css的position里的relative和absolute的区别
position有以下属性:static.inherit.fixed.absolute.relative前三个好理解好区分:static:是默认状态,没有定位,元素出现在正常的流中(忽略 top, b ...
- npm 安装 不快的解决办法
npm config list 查看配置 npm config set prefix “c:\dev\nvm\npm”(配置用npm下载包时全局安装的包路径) npm install npm -g ...
- dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象
dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象
- 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp
题目描述 给出一个有 $2^n$ 个叶子节点的完全二叉树.每个叶子节点可以选择黑白两种颜色. 对于每个非叶子节点左子树中的叶子节点 $i$ 和右子树中的叶子节点 $j$ :如果 $i$ 和 $j$ 的 ...
- IOS8模糊毛玻璃的效果UIVisualEffectView
UIVisualEffectView实现两种模糊效果:UIBlurEffect 和 UIVibrancyEffect 两者都是继承自UIView,前者放在任意的View里边都能对下册的视图渲染出模糊效 ...
- [学习笔记]搜索——模拟与dp的结合
搜索: 一种基础的算法. 考察常见于NOIP 但是高级的搜索算法可能还会在省选出现. 50%以上的暴力都可以用搜索直接枚举来写. 但是,当数据规模不是很大的时候,搜索也可能成为正解. (比如剪枝PK状 ...