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... ...
随机推荐
- HTTP 协议中 URI 和 URL 有什么区别?
HTTP 协议中 URI 和 URL 有什么区别? HTTP = Hyper Text Transfer ProtocolURI = Universal Resource IdentifierURL ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/images/json: dial unix /var/run/docker.sock: conne
使用docker报如下错误信息: Got permission denied while trying to connect to the Docker daemon socket at unix:/ ...
- dev初识 拖动分组
1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm ...
- 专题1-MMU-lesson3-MMU配置与使用
1.段方式MMU 利用虚拟地址然后找到物理地址,通过物理地址访问到led,其过程如下: 一个段的大小是[19:0]总共有1M的地址空间. 从上面可知对应GPIO的段物理基地址是0x7f000000.那 ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Maven——继承和聚合
实际项目中,可能正要构建一个大型的系统,但又不想一遍又一遍的重复同样的依赖元素,这种情况是经常出现的.不过还好,maven提供了继承机制,项目可以通过parent元素使用继承,可以避免这种重复.当一个 ...
- jQuery 演变史
一.说明 最近我读完了 jQuery 官方的博客仓库,目的是为了梳理清楚 jQuery API 接口的演变过程.从而明确知道在对应版本下使用正确.合适的 API,以下便是我的总结笔记. jQuery ...
- [GO]数组做函数参数
package main import "fmt" //数组为函数参数,实际上是值传递//实参数据里的每个元素,给形参数组拷贝一份//这里形参的数组其实就是实参的复制品 func ...
- App测试从入门到精通之更新测试
我们都知道,app在使用一段时间,都会有更新,而且更新会不止一次.在实际测试中,关于更新的测试场景也是我们需要重点关注的,接下来我们就看一下关于App的更新测试有哪些测试点我们需要注意: APP更新测 ...