Codeforces914E. Palindromes in a Tree
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的更多相关文章
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- 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 ...
- CF914E Palindromes in a Tree
$ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...
- CF914E Palindromes in a Tree(点分治)
link 题目大意:给定一个n个点的树,每个点都有一个字符(a-t,20个字符) 我们称一个路径是神犇的,当这个路径上所有点的字母的某个排列是回文 求出对于每个点,求出经过他的神犇路径的数量 题解: ...
- CF914E Palindromes in a Tree(点分治)
题面 洛谷 CF 题解 题意:给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个对应字母的排列为回文. ...
- CodeChef Tree Palindromes
Tree Palindromes Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin cha ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- ghj1222被坑记录[不持续更新]
考试注意事项:link1 link2 (密码:wangle) 调不出来bug,可以先透彻一会儿或者是上个厕所或者坐一会别的题(间隔至少20min),然后通读代码 -1. 考试先读题,读题之后搞出一个做 ...
随机推荐
- mysql多表查询20题
+-------------------+| Tables_in_dapeng3 |+-------------------+| class || course || s1 || score || s ...
- 自动创建xml文档
自动创建xml文档 import xml.etree.ElementTree as ET print(dir(ET)) #ET里面有Element方法 root = ET.Element(" ...
- 在一台电脑上运行两个或多个tomcat
在一台电脑上运行多个tomcat 在本例中,使用两个tomcat做示例 工具/原料 tomcat 安装好jdk,并且配置好环境变量 方法/步骤 首先去apache下载一个tomcat, ...
- python笔记_magic变量和函数
前言 先扯一点背景知识 PEP8(Python Enhancement Proposal)是一份python的编码规范,链接:http://www.python.org/dev/peps/pep-00 ...
- linux AC的应用详解
NAME ac - 输出用户连接时间 总览 ac [ -d | --daily-totals ] [ -y | --print-year ] [ -p | --individual-totals ] ...
- 【整理】用JSON-server模拟REST API
用JSON-server模拟REST API https://www.cnblogs.com/ys-wuhan/p/6387791.html
- 包含绑定变量的sql进行调优需注意一点
拿1个sql举个例子,我只贴出了where后面部分 实际环境中有init_date 和direct_no的组合索引IDX_DATE_NO 上诉标红处,:b3=0 和:b3<>0这两种情况o ...
- 用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化
http://blog.csdn.net/baiyu9821179/article/details/53365476 a=([3.234,34,3.777,6.33]) a为python的list类型 ...
- Bézier surface(贝塞尔曲面)
Bézier surface(贝赛尔曲面) 贝塞尔曲面是一种用于计算机图形学.计算机辅助设计和有限元建模的数学样条.与贝塞尔曲线一样,贝塞尔曲面由一组控制点定义.与插值在许多方面相似,一个关键的区别是 ...
- bzoj4951 [Wf2017]Money for Nothing
题目描述 题解: 答案显然是$max((q-p)*(e-d))$ 依然先贪心. 对于工厂,我们倾向于$pi<pj,di<dj$的; 对于买家,我们倾向于$qi>qj,ei>ej ...