Prelude

很好的模板题。

传送到Codeforces:(* ̄3 ̄)╭


Solution

首先要会DSU on Tree,不会的看这里:(❤ ω ❤)

众所周知DSU on Tree是可以用来处理子树信息的,但是有时候也可以用来处理链上信息。

IOI 2011 Race是一道著名的点分治模板题,要求统计链信息,也可以用DSU on Tree来做,题目链接在这里:(✿◕‿◕✿)

基本思路和点分治是一样的,对于每个点u,我们统计出所有经过u的路径的信息。

于是我们有了一个非常好的思路:统计每个点u的时候,我们记录下u的所有子孙节点到u的信息,放在某个数组里面。

以这道题为例子,我们把每个字符串压缩为一个二进制串,然后就可以记录u的每个后继节点到u的路径所形成的字符串。

但是问题来了,我们要保留重儿子的信息,但是节点u和她的重儿子之间有一个字母,我们要把这个字母加到重儿子的所有后继节点上,这不是就退化成了暴力了么?

对于IOI 2011 Race这样的题,我们可以选择用数据结构维护,于是复杂度多了一个log。

当然还有更简单的做法,对于本题和IOI 2011 Race这样的题,链上的信息是可减的,于是我们可以不保存“后继节点到点u”的信息,而是保存“后继节点到根”的信息,然后在统计的时候再减去“u到根的信息”。

每个节点到根的信息是不会变的,就不需要维护了,又因为路径信息可减,所以处理起来也很方便。

当然,对于不可减的路径信息,可以选择用数据结构维护。

当然,如果维护不了的话还是写好写好调的点分治吧qwq。


Code

#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int _w; int n, ans[MAXN]; namespace G {
int head[MAXN], nxt[MAXN], to[MAXN], val[MAXN], eid;
void init() {
memset(head, -1, sizeof head);
}
void adde( int u, int v, int w ) {
to[eid] = v, val[eid] = w;
nxt[eid] = head[u], head[u] = eid++;
}
} int sz[MAXN], dep[MAXN], pa[MAXN], son[MAXN], str[MAXN];
int prelude( int u, int fa, int d, int s ) {
using namespace G;
sz[u] = 1, dep[u] = d, pa[u] = fa, str[u] = s;
for( int i = head[u]; ~i; i = nxt[i] ) {
int v = to[i], t = 1<<val[i];
sz[u] += prelude(v, u, d+1, s^t);
if( sz[v] > sz[son[u]] )
son[u] = v;
}
return sz[u];
} int maxd[1<<22]; void ans_node( int s, int d, int &ans ) {
ans = max(ans, d + maxd[s]);
for( int i = 0; i < 22; ++i )
ans = max(ans, d + maxd[s^(1<<i)]);
}
void dfs_ans( int u, int &ans ) {
using namespace G;
ans_node(str[u], dep[u], ans);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_ans(to[i], ans);
} void ins_node( int s, int d ) {
maxd[s] = max(maxd[s], d);
}
void dfs_ins( int u ) {
using namespace G;
ins_node(str[u], dep[u]);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_ins(to[i]);
} void del_node( int s ) {
maxd[s] = -INF;
}
void dfs_del( int u ) {
using namespace G;
del_node(str[u]);
for( int i = head[u]; ~i; i = nxt[i] )
dfs_del(to[i]);
} void solve( int u, bool clr ) {
using namespace G;
for( int i = head[u]; ~i; i = nxt[i] )
if( to[i] != son[u] )
solve(to[i], 1);
if( son[u] ) solve(son[u], 0);
ans_node(str[u], dep[u], ans[u]);
ins_node(str[u], dep[u]);
for( int i = head[u]; ~i; i = nxt[i] )
if( to[i] != son[u] ) {
dfs_ans(to[i], ans[u]);
dfs_ins(to[i]);
}
if( clr ) dfs_del(u);
ans[u] -= dep[u] + dep[u];
for( int i = head[u]; ~i; i = nxt[i] )
ans[u] = max(ans[u], ans[to[i]]);
ans[u] = max(ans[u], 0);
} int main() {
_w = scanf( "%d", &n );
G::init();
for( int i = 2; i <= n; ++i ) {
int pa;
char ch;
_w = scanf( "%d %c", &pa, &ch );
G::adde(pa, i, ch - 'a');
}
for( int i = 0; i < (1<<22); ++i )
maxd[i] = -INF;
prelude(1, 0, 1, 0), solve(1, 0);
for( int i = 1; i <= n; ++i )
printf( "%d ", ans[i] );
puts("");
return 0;
}

【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree的更多相关文章

  1. dsu on tree学习笔记

    前言 一次模拟赛的\(T3\):传送门 只会\(O(n^2)\)的我就\(gg\)了,并且对于题解提供的\(\text{dsu on tree}\)的做法一脸懵逼. 看网上的其他大佬写的笔记,我自己画 ...

  2. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  3. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]

    D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...

  4. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  5. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 好像这个题只能Dsu On Tree? 有根树点分治 统计子树过x的 ...

  6. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    题目链接:Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ o ...

  7. CF 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths http://codeforces.com/problemset/probl ...

  8. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

  9. CF741 D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    题目意思很清楚了吧,那么我们从重排回文串的性质入手. 很容易得出,只要所有字符出现的次数都为偶数,或者有且只有一个字符出现为奇数就满足要求了. 然后想到什么,Hash?大可不必,可以发现字符\(\in ...

随机推荐

  1. HotSpot JVM 常用配置设置

    本文讨论的选项是针对HotSpot虚拟机的. 1.选项分类及语法 HotspotJVM提供以下三大类选项: 1.1.标准选项 这类选项的功能是很稳定的,在后续版本中也不太会发生变化. 运行java或者 ...

  2. ORM PHP 学习记录

    ORM:object relation mapping,即对象关系映射,简单的说就是对象模型和关系模型的一种映射.为什么要有这么一个映射?很简单,因为现在的开发语言基本都是oop的,但是传统的数据库却 ...

  3. b5

    吴晓晖(组长) 过去两天完成了哪些任务 完善推荐算法 展示GitHub当日代码/文档签入记录 接下来的计划 推荐算法 还剩下哪些任务 组员:刘帅珍 过去两天完成了哪些任务: 修改原型,整理背景 明日计 ...

  4. 预则立&&他山之石--团队计划、访谈优秀前辈

    团队计划&访谈内容 一.团队计划 序号 任务内容 计划完成时间 主要负责人 备注 1 对接教师报课系统 决定是否重构代码 2016.10.16 陈少铭.黄家俊 阅读CourseManageme ...

  5. Unity3d学习日记(六)

      今天在研究怎么在unity中将image上的图片保存到本地,主要参考下面两个链接:Unity Texture2D缩放.UNITY存储图片到本地   结合上述两个链接,我写了如下代码来将缩放后或者改 ...

  6. 结对编程:四则运算。组员:闫浩楠 杨钰宁 开发语言:C语言

    需求分析:1.能够自动出题并给出答案 2.包含“+,—,*,/,()” 的四则运算. 3.显示题目的答案 结构设计:1.自动出题用随机数生成语句实现:包括随机生成数字.运算符号和题目长度 2.用变量约 ...

  7. 简单说明webbench的安装和使用

    简介 运行在linux上的一个性能测试工具 官网地址:http://home.tiscali.cz/~cz210552/webbench.html 如果不能打开的话,也可以直接到网盘下载:http:/ ...

  8. sguf冲销脚本的实现

    1.该脚本为PCISS项目的sguf冲销脚本: DECLARE type typ_sguf_table is table of sguf_rowid_tab_1%rowtype ; sguf_tab ...

  9. Ubuntu安装使用中的一些注意事项

    在win7上安装VMware workstations10.0 ,在VMware workstations10.0上安装Ubuntu14.04 64位时,关于网络的连接注意: win7 网络连接里上的 ...

  10. ACM数论之旅8---组合数(组合大法好(,,• ₃ •,,) )

    组合数并不陌生(´・ω・`) 我们都学过组合数 会求组合数吗 一般我们用杨辉三角性质 杨辉三角上的每一个数字都等于它的左上方和右上方的和(除了边界) 第n行,第m个就是,就是C(n, m) (从0开始 ...