n<=100000的树,每个点上有个字母a-t之一,问有多少这样的链经过每个点:它的某一个排列的字母串起来是回文的。

就是有最多一个字母是奇数个啦。。这样点分算一波即可。。细节较多详见代码

 #include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
//#include<queue>
//#include<iostream>
using namespace std; int n;
#define maxn 200011
struct Edge{int to,next;}edge[maxn<<]; int first[maxn],le=,val[maxn]; char s[maxn];
void in(int x,int y) {Edge &e=edge[le]; e.to=y; e.next=first[x]; first[x]=le++;}
void insert(int x,int y) {in(x,y); in(y,x);} #define maxs 1111111
int cnt[maxs]; #define LL long long
LL ans[maxn]; int size[maxn]; bool die[maxn];
void getsize(int x,int fa)
{
size[x]=;
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (e.to==fa || die[e.to]) continue;
getsize(e.to,x); size[x]+=size[e.to];
}
} int getroot(int x,int fa,int tot)
{
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (e.to==fa || die[e.to]) continue;
if (size[e.to]*>tot) return getroot(e.to,x,tot);
}
return x;
} void dfscnt(int x,int fa,int now,int v)
{
now^=<<val[x]; cnt[now]+=v;
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (e.to==fa || die[e.to]) continue;
dfscnt(e.to,x,now,v);
}
} LL calc(int x,int fa,int now)
{
now^=<<val[x]; LL t=;
for (int i=;i<;i++) t+=cnt[now^(<<i)];
t+=cnt[now];
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (e.to==fa || die[e.to]) continue;
t+=calc(e.to,x,now);
}
ans[x]+=t; return t;
} void cd(int x)
{
dfscnt(x,,,); die[x]=;
LL now=;
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (die[e.to]) continue;
dfscnt(e.to,x,<<val[x],-);
now+=calc(e.to,x,); //cout<<e.to<<' '<<now<<endl;
dfscnt(e.to,x,<<val[x],);
} for (int i=;i<;i++) now+=cnt[<<i];
now+=cnt[]+; ans[x]+=now>>;
dfscnt(x,,,-);
// cout<<x<<endl;
// for (int i=1;i<=n;i++) cout<<ans[i]<<' ';cout<<endl;
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (die[e.to]) continue;
getsize(e.to,); cd(getroot(e.to,,size[e.to]));
}
} int main()
{
scanf("%d",&n);
for (int i=,x,y;i<n;i++) scanf("%d%d",&x,&y),insert(x,y);
scanf("%s",s+); for (int i=;i<=n;i++) val[i]=s[i]-'a';
getsize(,);
cd(getroot(,,size[]));
for (int i=;i<=n;i++) printf("%lld ",ans[i]);
return ;
}

Codeforces914E. Palindromes in a Tree的更多相关文章

  1. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  2. codeforces 914E Palindromes in a Tree(点分治)

    You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...

  3. Palindromes in a Tree CodeForces - 914E

    https://vjudge.net/problem/CodeForces-914E 点分就没一道不卡常的? 卡常记录: 1.把不知道为什么设的(unordered_map)s换成了(int[])s ...

  4. CF914E Palindromes in a Tree

    $ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...

  5. CF914E Palindromes in a Tree(点分治)

    link 题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符) 我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文 求出对于每个点,求出经过他的神犇路径的数量 题解: ...

  6. CF914E Palindromes in a Tree(点分治)

    题面 洛谷 CF 题解 题意:给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个对应字母的排列为回文. ...

  7. CodeChef Tree Palindromes

    Tree Palindromes Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin cha ...

  8. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  9. ghj1222被坑记录[不持续更新]

    考试注意事项:link1 link2 (密码:wangle) 调不出来bug,可以先透彻一会儿或者是上个厕所或者坐一会别的题(间隔至少20min),然后通读代码 -1. 考试先读题,读题之后搞出一个做 ...

随机推荐

  1. Spring Boot学到的内容

    Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...

  2. jQuery中面向对象思想实现盒子内容切换

    这里主要是模拟小米官网中的首页的内容模块实现的主要动态效果 布局:采用了bootstrap框架进行布局,及其其中的字体图标 html: <!-- 内容 --> <div class= ...

  3. python pandas 中 loc & iloc 用法区别

    转自:https://blog.csdn.net/qq_21840201/article/details/80725433 ### 随机生DataFrame 类型数据import pandas as ...

  4. OCP 11g 第四章练习

    练习 4-1 配置Oracle Net 在本练习中, 将使用图形化工具和命令行工具来建立一个完整的Oracle Net 环境. 由此, 读者可看出在Windows 和 Linux 系统中的区别. 1. ...

  5. 解决./mysql-bin.index’ not found (Errcode: 13)

    问题出现在升级php版本以后,网站无法连接数据库,phpMyAdmin无法登录: 然后尝试开启mysql,/etc/init.d/mysqld start ,提示: Starting MySQL. E ...

  6. std::map插入已存在的key时,key对应的内容不会被更新

    std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...

  7. 推荐一个高大上的网易云音乐命令行播放工具:musicbox

    网易云音乐上有很多适合程序猿的歌单,但是今天文章介绍的不是这些适合程序员工作时听的歌,而是一个用Python开发的开源播放器,专门适用于网易云音乐的播放.这个播放器的名称为MusicBox, 特色是用 ...

  8. Spring AOP源码解析——专治你不会看源码的坏毛病!

    昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点.太他爷爷的有道理了!要说看人品,还是女孩子强一些. 原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子.哥哥们, ...

  9. Windows SubSystem for Linux(WSL)设置默认和设置默认登陆用户

    使用wslconfig命令进行管理 1.  设置默认运行的linux系统 wslconfig /setdefault <DistributionName> 正如上面所说,如果执行wslco ...

  10. <a>标签的href 与 onclick 使用

    链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行,on ...