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 ...
随机推荐
- 三张图教你生成一个Android jar 库。
我看到非常多教人使用第三方开源组件的Android教程.都是在教基于源代码project的库导入,个人觉得非常不妥,觉得最恰当的方式是把源代码project生成一个jar再导入到目标project上使 ...
- lonlifeOJ1152 “玲珑杯”ACM比赛 Round #19 概率DP
E -- Expected value of the expression DESCRIPTION You are given an expression: A0O1A1O2A2⋯OnAnA0O1A1 ...
- 常用的sql命令
1 mysql创建数据库 create database [database name]; 2 创建表 create table [table name]([first column name] [f ...
- Linux系统(Centos)下安装Java环境配置步骤详述
1.首先要去下载好JDK,Java SE 8的官方网址是http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2 ...
- Easy smart REST with kbmMW
使用新版kbmMW开发的 smart service,也可以轻松的发布为通过REST来调用的功能. 一个 kbmMW smart service象下面这样实现,就可以使用REST来访问: ty ...
- uboot配置和编译过程详解【转】
本文转载自:http://blog.csdn.net/czg13548930186/article/details/53434566 uboot主Makefile分析1 1.uboot version ...
- YTU 2452: 麦克劳林用于函数求值
2452: 麦克劳林用于函数求值 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 12 题目描述 泰勒公式是一个用函数在某点的信息描述其附近取值的公式.如果函数足够光滑的话 ...
- lock的两种方式
假设现在我们有100个数据项可以读写.有若干个线程,任何一个线程可能对任何一个数据项尽心读写. 但是,如果不同的线程在对同一个数据项进行读写,就可能发生错误.需要使用lock进行控制. 比如线程x要对 ...
- hdu2552
点击打开链接 思路: 1.tan(a+b) = ( tan(a) + tan(b) ) / (1 – tan(a) * tan(b) ) 2.tan( atan(x) ) = x arctan(1/s ...
- hdu-5720 Wool(区间并+扫描线)
题目链接: Wool Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Pr ...