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... ...
随机推荐
- 8-python模拟登入(无验证码)
方式: 1.手动登入,获取cookie 2.使用cookielib库 和 HTTPCookieProcessor处理器 #_*_ coding: utf-8 _*_ ''' Created on 20 ...
- CF407B Long Path
好玩的题. 首先我们(看一下题解之后)发现当你第一次走到了一个点的时候,那么它之前的所有点一定都访问过了偶数次. 假设我们第一次走到了一个点$i$,那么$i - 1$一定访问了偶数次,那么第一次走$i ...
- C#中对DataTable进行全连接后group by,orderby
var result = from temp2 in ( from u in u ...
- Selenium如何定位动态id/class的元素?
今天再给大家分享一个定位不到元素的原因——动态id. 没有打开新页面.没有alert.没有frame.加了等待时间,但是还是定位不到元素?很有可能是你要定位的元素的属性是动态的,即每次打开页面,这个 ...
- iOS CocoaPods安装与使用 好东西保存
http://www.cnblogs.com/daguo/p/4097263.html http://www.cnblogs.com/pican/p/3939941.html?utm_source=t ...
- C#中特殊的string类型
string C#有string关键字,在翻 ...
- MySQL性能调优与架构设计——第7章 MySQL数据库锁定机制
第7章 MySQL数据库锁定机制 前言: 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核 ...
- ubuntu 14.04 x64下安装libreoffice
LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...
- angular 服务之间依赖注入
import { Injectable } from '@angular/core'; @Injectable() export class LoggerServiceService { constr ...
- SQL 全角半角转换-(摘抄)
/****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...