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. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  2. 赵雅智_SimpleCursorAdapter

    项目步骤 声明listView控件并获取显示的视图 获取显示的数据 设置显示的adapter 注冊点击事件 详细案例 实现效果: watermark/2/text/aHR0cDovL2Jsb2cuY3 ...

  3. feed流,图片在左还是右的区别是

    feed流设计:那些谋杀你时间APP | 人人都是产品经理 http://www.woshipm.com/pd/773523.html

  4. 【独立开发人员er Cocos2d-x实战 007】使用Cocos2dx UserDefault.xml

    这篇博客是因为下述问题产生的 -(代码1): std::string str = FileUtils::getInstance()->getWritablePath(); CCLOG(str.c ...

  5. 新建web项目时css注意事项

    初始化css ,如设置body的margin,padding值,button:hover的pointer手型,li dd的list-style,a的下划线等. 最好将常用的初始化css文件整合在一起, ...

  6. org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误

    当遇到如下错误的时候 可能以为是regionserver 挂掉或者其他原因导致连接不上regionserver  但后面提示了Hbase 表statistic_login 具体信息 Thu Jan 1 ...

  7. SPOJ:OR(位运算&数学期望)

    Given an array of N integers A1, A2, A3…AN. If you randomly choose two indexes i ,j such that 1 ≤ i ...

  8. POJ3020 二分图匹配——最小路径覆盖

    Description The Global Aerial Research Centre has been allotted the task of building the fifth gener ...

  9. 「BZOJ3438」小M的作物(最小割

    3438: 小M的作物 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1891  Solved: 801[Submit][Status][Discus ...

  10. html&css题

    1.对WEB标准以及W3C的理解与认识?(1)web标准规范要求,书写标签必须闭合.标签小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率:(2)建议使用外链css和js脚本,从而达到结构与行为.结 ...