Palindromes in a Tree CodeForces - 914E
https://vjudge.net/problem/CodeForces-914E
点分就没一道不卡常的?

卡常记录:
1.把不知道为什么设的(unordered_map)s换成了(int[])s
2.减少一次cal2和clr
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pi;
struct E
{
int to,nxt;
}e[];
int f1[],ne;
int sz[],fx[],d[];ll ans[],ta,an1[];
char ss[];int a[];
bool vis[];
int root,sum;
int lft[];
int s[];
void getroot(int x,int fa)
{
sz[x]=;fx[x]=;
for(int k=f1[x];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
getroot(e[k].to,x);
sz[x]+=sz[e[k].to];
fx[x]=max(fx[x],sz[e[k].to]);
}
fx[x]=max(fx[x],sum-sz[x]);
if(fx[x]<fx[root]) root=x;
}
void getsz(int x,int fa)
{
sz[x]=;
for(int k=f1[x];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
getsz(e[k].to,x);
sz[x]+=sz[e[k].to];
}
}
void calc(int u,int fa)
{
an1[u]+=s[d[u]^a[root]],ta+=s[d[u]^a[root]];
for(int i=;i<;i++) an1[u]+=s[d[u]^a[root]^lft[i]],ta+=s[d[u]^a[root]^lft[i]];
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
calc(e[k].to,u);
}
void addd(int u,int fa)
{
s[d[u]]++;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
d[e[k].to]=d[u]^a[e[k].to];
addd(e[k].to,u);
}
}
void deld(int u,int fa)
{
s[d[u]]--;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
deld(e[k].to,u);
}
void cal2(int u,int fa)
{
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
{
cal2(e[k].to,u);
an1[u]+=an1[e[k].to];
}
ans[u]+=an1[u];
}
void clr(int u,int fa)
{
an1[u]=;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to]&&e[k].to!=fa)
clr(e[k].to,u);
}
void solve(int u)
{
d[u]=;vis[u]=;ta=;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
{
d[e[k].to]=a[e[k].to];
addd(e[k].to,u);
}
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
{
clr(e[k].to,u);
deld(e[k].to,u);
calc(e[k].to,u);
addd(e[k].to,u);
}
ans[u]+=ta/;ta=;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
deld(e[k].to,u);
s[]++;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
{
calc(e[k].to,u);
cal2(e[k].to,u);
}
s[]--;ans[u]+=ta;
for(int k=f1[u];k;k=e[k].nxt)
if(!vis[e[k].to])
{
root=;getsz(e[k].to,);sum=sz[e[k].to];
getroot(e[k].to,);solve(root);
}
}
int n;
int main()
{
fx[]=0x3f3f3f3f;
int i,x,y;
lft[]=;
for(i=;i<=;i++) lft[i]=lft[i-]<<;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
e[++ne].to=y;e[ne].nxt=f1[x];f1[x]=ne;
e[++ne].to=x;e[ne].nxt=f1[y];f1[y]=ne;
}
scanf("%s",ss+);
for(i=;i<=n;i++) a[i]=lft[ss[i]-'a'];
root=;sum=n;getroot(,);
solve(root);
for(i=;i<=n;i++) printf("%lld ",ans[i]+);
return ;
}
Palindromes in a Tree CodeForces - 914E的更多相关文章
- 【CodeForces】914 E. Palindromes in a Tree 点分治
[题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- codeforces 914E Palindromes in a Tree(点分治)
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- B. Grow The Tree Codeforces Round #594 (Div. 2)
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on t ...
随机推荐
- Linux input子系统实例分析(一)
这是一个简单的输入设备驱动实例.这个输入设备只有一个按键,按键被连接到一条中断线上,当按键被按下时,将产生一个中断,内核将检测到这个中断,并对其进行处理.该实例的代码如下: 1: #inclu ...
- [网页游戏开发]Morn组件赋值
在讲解List之前,我们先介绍一下Morn组件赋值功能 默认属性赋值 界面逻辑开发过程中,经常会涉及到动态更改UI属性,比如: 界面有一个按钮,一个多选框和一个文本,分别命名为myButton,myC ...
- WPF新手之如何将数据绑定到TreeView
看过许多例子,全是绑定到类的,没人说如何绑定到某个对象,偏偏我这个绝对的新手就是要绑定到一个对象,只能自己摸索了: 首先要将数据绑定到容器,有以下几个默认条件:①元数据必须包装在List或者Obser ...
- fatal error C1083: 无法打开预编译头文件:“Debug\opencv.pch”: No such file or directory
stdafx.cpp右键——属性,预编译头选“创建”,其它cpp选“使用”. 调试不能优化.
- min-width 和 @media screen
min-width可以容器设置最小宽度,低于改宽度时,会自动加上滚动条,支持ie7及ie7+: @media only screen and (min-width: /*最小宽度(要加单位px)*/) ...
- IRRIGATION LINES
IRRIGATION LINES Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB Total submit us ...
- e.target与e.currentTarget的区别
在DOM事件对象中有两个属性总是时不时的困扰我,就是target和currentTarget,有时候很迷惑分不清两者的区别,因此有必要把这两个属性好好梳理一下,加深理解,以便日后的查询. MDN中对t ...
- IntelliJ IDEA 注册码 有效期截止于2018/10/14
来源: http://idea.lanyus.com/ IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW ...
- MYSQL进阶学习笔记十二:MySQL 表分区!(视频序号:进阶_29,30)
知识点十三:MySQL 表的分区(29) 一.什么要采用分区: 分区的定义: 当数据量过大的时候(通常是指百万级或千万级数据的时候),这时候需要将一张表划分几张表存储.一些查询可以得到极大的优化,这主 ...
- React引入,运行
1.引入 <script src="https://cdn.bootcss.com/react/15.5.4/react.min.js"></script> ...