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. 设计模式(3)-- 原型模式 (clone分析)

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建对象. 在java中有语言级别的支持:clone 在java中使用原型模式是非常简单的事情,实现Cloneable接口,调用Objec ...

  2. poj2139 Six Degrees of Cowvin Bacon

    思路: floyd 实现: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  3. Dynamic Median

    题意: 设计一个数据结构,初始为空,支持以下操作: (1)增加一个元素,要求在log(n)时间内完成,其中n是该数据结构中当前元素的个数.注意:数据结构中允许有重复的元素. (2)返回当前元素集合的中 ...

  4. Linux 从源码编译安装 Nginx

    Nginx 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器.Nginx 编译安装比较简单,难点在于配置.下面是 Nignx 0.8.54 编译安装和简 ...

  5. word打印小册子

    使用联想m7250f打印册子,打印时设置该打印机属性为双面打印(手动),打印第一面后,将所有打印出的纸拿出并翻转使对应word中的第2页的打印纸朝外,之后将所有纸放入纸盒,再点击打印第二面即可.

  6. c# 从DataGridVieew导出到excel

    public static bool DataGridViewToExcel(DataGridView dataGridView, bool isShowExcel) { int rowsQty = ...

  7. vijos 1053 Easy sssp

    描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...

  8. jeecms

    ===标签=== <!-- 显示一级栏目对应的二级栏目 --> <!-- [@cms_channel_list parentId=c.id] [#if tag_list?size&g ...

  9. JavaScript/JQuery radioButton(单选按钮)练习20190409

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. IntelliJ IDEA集成工具Database连接MySQL8.0报错的解决方法

    直接用默认配置连接的话,会报以下错误: Connection to MySQL - @localhost failed. [08001] Could not create connection to ...