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. 考试先读题,读题之后搞出一个做 ...
随机推荐
- iOS 创建xcode插件
苹果的"一个足以应付所有"策略使得它的产品越来越像一个难以下咽的药丸.尽管苹果已经将一些工作流带给了iOS/OS X的开发者,我们仍然希望通过插件来使得Xcode更加顺手! 虽然苹 ...
- Performance engineering introduction
1.Performance Software performance is one of software quality attributes, it equal to efficiency w ...
- Clean Code 第十章 : 类
最近的CleanCode读到了第十章.这一张主要讲了如何去构造一个类,感觉的CleanCode至此已经不仅仅是单纯的讲如何'写'出漂亮的代码,而是从设计方向上去构造出好的代码了. 本章节主要讲了: * ...
- Visual studio每次build自动增加版本号
关键词:visual studio,rc file,VS_VERSION_INFO,FILEVERSION,PRODUCTVERSION 目标:希望每次在vs中编译项目时,生成的可执行程序版本号自动+ ...
- java 读取txt,java读取大文件
java 读取txt,java读取大文件 package com.bbcmart.util; import java.io.File;import java.io.RandomAccessFile;i ...
- PYTHON PIP和kivy安装教程
我们安装pip.我们同样需要在Python的官网上去下载 下载地址:https://pypi.python.org/pypi/pip 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: ...
- Android(java)学习笔记174:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService: oncreate() ---> onstartCommand() ---& ...
- 使用sersync实现实时同步实战
场景需求: 应用程序会在机器192.168.2.2 /usr/local/news目录中生成一些数据文件,现在需要实时同步到主机192.168.3.3/usr/local/www/cn/news中,同 ...
- 网新恩普(W 笔试)
选择题 1.一桶有黄色,绿色,红色三种,闭上眼睛抓取同种颜色的两个.抓取多少个就可以确定你肯定有两个同一颜色的球? 答案: 4次 1.最坏打算抓3次都是不同颜色的黄.绿.红,此时,三种颜色的球各抓了一 ...
- 【转载】用Python实现端口映射功能(A/B/C内外网)
转载地址 :http://hutaow.com/blog/2014/09/08/write-tcp-mapping-program-with-python/ 有A,B,C三台计算机,A,B互通,B,C ...