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. 考试先读题,读题之后搞出一个做 ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(10)——this
接着上一节讲的话,应该轮到“执行上下文栈”了,但是这里不得不插入一节,把this说一下.因为this很重要,js的面试题如果不出几个与this有关的,那出题者都不合格. 其实,this的取值,分四种情 ...
- 类似QQ在线离线好友界面
把头像设置成圆形的代码如下: package com.example.lesson6_11_id19; import android.content.Context; import android.c ...
- codevs 1979 第K个数
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字 ...
- OFDM、FTTx、SCTP、Ad Hoc、WSN术语简介
上课提到一些术语,下来查了一下,总结在这里. OFDM: OFDM(Orthogonal Frequency Division Multiplexing)即正交频分复用技术,实际上OFDM是MCM(M ...
- MySQL系列(二)--MySQL存储引擎
影响数据库性能的因素: 1.硬件环境:CPU.内存.存盘IO.网卡流量等 2.存储引擎的选择 3.数据库参数配置(影响最大) 4.数据库结构设计和SQL语句 MySQL采用插件式存储引擎,可以自行选择 ...
- 沈南鹏@《遇见大咖》: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
沈南鹏@<遇见大咖>: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
- 优化UITableView
在iOS应用中,UITableView应该是使用率最高的视图之一了.iPod.时钟.日历.备忘录.Mail.天气.照片.电话.短信.Safari.App Store.iTunes.Game Cente ...
- strong&weak
copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协议 ...
- Shell替换数组元素之间的间隔符号
Shell中的数组是这样表示的: arr=(1,2,3,4,5) 它们数组元素的间隔符号为逗号,如果我相把逗号替换为加号: echo ${arr//,/+} 输出: 1+2+3+4+5 这个表达式是我 ...
- 文本三剑客之grep
接受正则表达式,按行匹配,将会过滤出匹配的所有行 格式: grep [OPTION]... PATTERN [FILE]... 可以看出,grep后可以同时接多个文件 选项OPTIO ...