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的更多相关文章

  1. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  2. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  3. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

  4. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  5. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  6. codeforces 914E Palindromes in a Tree(点分治)

    You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered fro ...

  7. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  8. AC日记——Propagating tree Codeforces 383c

    C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. B. Grow The Tree Codeforces Round #594 (Div. 2)

    Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on t ...

随机推荐

  1. java面试题(摘录)

    1.抽象,继承,封装,多态 2.基本数据类型的字节数 byte:1.int:4.char:2.long:8.float:4.double:8.boolean:1 和short:2 3.String , ...

  2. $.ajax 使用详解

    Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作. $.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() ...

  3. poj2761静态区间第k大

    例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...

  4. POJ3984 迷宫问题 —— BFS

    题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. java 开发面试题小整理(二)

    51.Anonymous Inner Class(匿名内部类)是否可以继承其它类?是否可以实现接口? 答:可以继承其他类或实现其他接口,在Swing编程和Android开发中常用此方式来实现事件监听和 ...

  6. SpringMVC配置环境

    一,lib目录下加入spring一般所需的jar包 二,配置web.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  7. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

  8. Oracle :修改数据库服务器字符集

    最近,有现场反应,程序显示乱码.感觉很奇怪,该系统已经卖出去无数了.肯定是现场数据库字符集有问题,经过查看, 现场环境: window系统,oracle10g. 我们要求的数据库字符集是AL32UTF ...

  9. I.MX6 不一定要设置BOOT_MODE进入烧录模式

    /************************************************************************* * I.MX6 不一定要设置BOOT_MODE进入 ...

  10. 【字符串】BZOJ上面几个AC自动机求最为字串出现次数的题目

    (一下只供自己复习用,目的是对比这几个题,所以写得不详细.需要细节的可以参考其他博主) [BZOJ3172:单词] 题目: 某人读论文,一篇论文是由许多(N)单词组成.但他发现一个单词会在论文中出现很 ...