【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 ...
随机推荐
- lintcode-512-解码方法
512-解码方法 有一个消息包含A-Z通过以下规则编码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 现在给你一个加密过后的消息,问有几种解码的方式 样例 给你的消 ...
- lintcode-424-逆波兰表达式求值
424-逆波兰表达式求值 求逆波兰表达式的值. 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2" ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- log4j配置独立日志方法
不使用类,而是使用loggerName来创建日志: #json是用java代码创建logger时用name,而不是jsonlog,注意,不需要在rootLogger中再配置,否则其它无关信息也将输出到 ...
- Java多线程 -join用法
阿里面试官问我这个问题,我仔细总结了一下: 参考:sleep.yield.wait.join的区别(阿里面试) 1. join()介绍 join() 定义在Thread.java中.join() 的作 ...
- Android自动化 -- sendevent/getevent 用法
getevent&sendevent 是android系统下的一个工具,可以 模拟 多种按键和触屏操作,产生的是raw event,raw event经过event hub处理产生最终的ges ...
- daemon not running的解决办法
有时候,当我们执行 adb devices 或者 adb start-server 的时候,会出现下面的情况: * daemon not running. starting it now on por ...
- day06 小数据池和编码
一. 上次课内容回顾字典:由{}括起来. 每个元素用逗号隔开, key:value的形式存储数据key: 不可变的. 可哈希的.增删改查:1. 增加: 直接用新key来赋值. dict[key] = ...
- [十四]SpringBoot 之 Spring拦截器(HandlerInterceptor)
过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截 ...
- DjangoORM字段参数介绍
参数介绍: 字段的参数: null: ->db是否可以为空 default: ->默认值 primary_key: ...