CF914E Palindromes in a Tree(点分治)
题面
题解
题意:给你一颗 n 个顶点的树(连通无环图)。顶点从 1 到 n 编号,并且每个顶点对应一个在‘a’到‘t’的字母。 树上的一条路径是回文是指至少有一个对应字母的排列为回文。 对于每个顶点,输出通过它的回文路径的数量。 注意:从u到v的路径与从v到u的路径视为相同,只计数一次
性质:回文字符串至多一个字母次数为奇数
因为字母只有'a'~'t'
那么可以状压一下
然后就是套点分治就好了
注意:顶点经过的次数要除以2(因为每条路径算了两次)
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
const int N = 200010;
inline int gi() {
RG int x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-'0', c = getchar();
return f ? -x : x;
}
struct node {
int to, next;
}g[N<<1];
int last[N], gl;
inline void add(int x, int y) {
g[++gl] = (node) {y, last[x]};
last[x] = gl;
return ;
}
int sum, f[N], siz[N], rt;
bool vis[N];
void getroot(int u, int fa) {
siz[u] = 1; f[u] = 0;
for (int i = last[u]; i; i = g[i].next) {
int v = g[i].to; if (v == fa || vis[v]) continue;
getroot(v, u);
f[u] = max(f[u], siz[v]);
siz[u] += siz[v];
}
f[u] = max(f[u], sum-siz[u]);
if (f[rt] > f[u]) rt = u;
return ;
}
char a[N];
int s[N], t[10050000];
void dfs(int x, int fa, int p, int S) {
t[S ^= (1 << s[x])] += p;
for (int i = last[x]; i; i = g[i].next) {
int v = g[i].to;
if (v == fa||vis[v]) continue;
dfs(v, x, p, S);
}
}
LL ans[N];
LL calc(int x, int fa, int S) {
S ^= (1 << s[x]);
LL cnt = t[S];//都为偶数个
for (int i = 0; i < 20; i++) cnt += t[S^(1<<i)];//出现了一个次数为奇数个
for (int i = last[x]; i; i = g[i].next) {
int v = g[i].to;
if (v == fa || vis[v]) continue;
cnt += calc(v, x, S);
}
ans[x] += cnt;
return cnt;
}
void solve(int x) {
vis[x] = 1;
dfs(x, 0, 1, 0);
LL cnt = t[0];
for (int i = 0; i < 20; i++) cnt += t[1<<i];
//单个一条链
for (int i = last[x]; i; i = g[i].next) {
int v = g[i].to; if (vis[v]) continue;
dfs(v, x, -1, 1<<s[x]);//去掉以v开头的链
cnt += calc(v, x, 0); //计算组合起来的路径
dfs(v, x, 1, 1<<s[x]);
}
dfs(x, 0, -1, 0);
ans[x] += cnt/2;//算了两次啦
for (int i = last[x]; i; i = g[i].next) {
int v = g[i].to; if(vis[v]) continue;
sum = siz[v];rt = 0;
getroot(v, 0);
solve(rt);
}
return ;
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n = gi();
for (int i = 1; i < n; i++) {
int u = gi(), v = gi();
add(u, v); add(v, u);
}
scanf("%s", a);
for (int i = 0; i < n; i++) s[i+1] = a[i]-'a';
f[0] = sum = n; rt = 0;
getroot(1, 0); solve(rt);
for (int i = 1; i <= n; i++)
printf("%lld ", ans[i]+1);
return 0;
}
CF914E Palindromes in a Tree(点分治)的更多相关文章
- CF914E Palindromes in a Tree(点分治)
link 题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符) 我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文 求出对于每个点,求出经过他的神犇路径的数量 题解: ...
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- 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... ...
随机推荐
- php格式化时间戳显示友好的时间
在项目中时间一律显示为2014-10-20 10:22显得很呆板.在微博.QQ空间等网站通常会显示为几秒前,几分钟前,几小时前等容易阅读的时间,我们称之为友好的时间格式.那么用php怎么实现呢? 大体 ...
- 用CSS3.0画圆
CSS3.0中有一个border-radius属性,这个属性允许向 div 元素添加圆角边框,也就是div边角不再一直是直角,在CSS3.0中可以做成圆角了,所以我们可以用这个属性用div画一个圆,或 ...
- Mysql处理海量数据时的一些优化查询速度方法(转)
最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当mysql表的数据量达到百万级时,普通SQL查询效率呈直线下降,而且如果whe ...
- xml解析中的DOM和SAX的区别
面试题:DMO和SAX的区别? DOM解析的优点:增删查改操作方便,缺点:占用内存较大,不适合解析大的XML文件: SAX解析的优点:占用内存小,解析快:缺点:不适合增删查改:
- 关于Pascal(帕斯卡)以及Camel(驼峰)命名法
小驼峰式命名法(lower camel case): 第一个单字以小写字母开始:第二个单字的首字母大写,例如:firstName.lastName,也被称为Camel命名法. 大驼峰式命名法(uppe ...
- MongoDB整理笔记の减少节点
当应用的压力小时,可以减少一些节点来减少硬件资源的成本:总之这是一个长期且持续的工作. 下面将刚刚添加的两个新节点28013 和28014 从复制集中去除掉,只需执行rs.remove 指令就可以了, ...
- delphi 取json中数组的值(ISuperArray)
{ "action": "******", "data": [ { "Info1": { "ID": ...
- webform Response的一些成员
1. Response.BufferOutPut,关闭缓冲区. 2. Response.Flush,一次性把缓冲区的内容释放出来. 3. Response.Clear,清空缓冲区. 4. Respon ...
- angular 服务之间依赖注入
import { Injectable } from '@angular/core'; @Injectable() export class LoggerServiceService { constr ...
- 在有主分支和个人分支情况下的TFS使用方法
从事.NET开发的资深童鞋一定都知道VS有自带的代码管理工具TFS(Team Foundation Server ),但是开发萌新可能就不太了解了,下面我就介绍一下这个工具以及它的一些常用操作. TF ...