我们必须抱有希望,这并不是因为希望真的存在,而是因为我们要做高贵的人。
【模板】树上前缀排序(雾)
题目描述
给定一颗以 $1$ 为根包含 $n$ 个节点的树,保证对于 $2$ ~ $n$ 的每个节点,其父亲的编号均小于自己的编号。
每个节点上有一个的字符,一个节点所代表的字符串定义为从根节点到这个节点的简单路径上经过的所有字符连起来形成的字符串。
请你给这些字符串按照字典序排序。
特别地,如果两个节点所代表的字符串完全相同,它们的大小由它们的父亲所代表的字符串的大小决定,如果仍相同,则由它们编号的大小决定。
输入输出格式
输入格式:
第 $1$ 行包含一个正整数 $n$。
第 $2$ 行包含 $n-1$ 个数 $f_{2 \dots n}$,其中 $f_i$ 表示 $i$ 的父亲。
第 $3$ 行为一个包含 $n$ 个小写字母的字符串 $s$,其中 $s_i$ 表示编号为 $i$ 的节点上的字符。
输出格式:
输出一行 $n$ 个正整数,第 $i$ 个正整数表示代表排名第 $i$ 的字符串的节点编号。
输入输出样例
输入样例#1:
5
1 1 3 2
abbaa
输出样例#1:
1 5 4 2 3
说明
对于 $20\%$ 的数据,$1 \le n \le 10 ^ 3$。
对于 $50\%$ 的数据,$1 \le n \le 10 ^ 5$。
对于 $100\%$ 的数据,$1 \le n \le 5 \times 10 ^ 5$。
题解
I may have a $O(n)$ ideal….
The set of character is really small
Firstly , if we have a trie , we could sort all the paths which are from the node to the root by DFS in $O(n)$ time.
So how could we get the tree ?
find every point’s father is given by the order of the number , the char of root is meaningless to the other paths….So we let the edge to the father own the char.
Each Node has a vector to save numbers , and a a array size of 26 to $O(1)$ check if the father has the $c_i$ or transfer to $c_i$
suppose the current point is $i$ , the char is $c_i$ , the father is $f_i$ , we use a array size of 26 to $O(1)$ check if the father has the $c_i$ , if the father has the $c_i$ , we push the number to the vector of the Node…
The correctness of this is same as the normal tree proof:
when father is the root , it’s apparently right .
else
if the node p , q have same char from the the father , we merge it..
consider the subtree.
let s is p’s son , t is q’s son , if $c(s) = c(t)$ , the situation of two node is right , the correctness is depend on sons.
if $c(s) \ne c(t) $ , it is ordinary situation , which depend on the correctness of father..
we used inductive method.
Emmmm , could you understand the proof above ?
Or we could prove the each path on given tree , has the only path on the tree we create.
So the complexity is $O(n)$ , code will be finished tomorrow because Toto ask me some inclusion-exclusion principle problems….
Writing the code…..
草我又发现我读错题了。我这实在堪忧。错误的题意:从根到节点,正确的是:节点到根。
错误题意的代码(也许可以出一道联赛模拟题?)
1 |
|
其实这个错误的题意还是很有意思的。
真模板好像就是广义SAM了吧,插入特殊字符然后分配所有后缀标记然后DFS一遍就是线性的?
树上游戏
每个节点有一个颜色,定义$s(i,j)$表示$i$到$j$的颜色数。
求出对于每个$i$ , $\sum\limits_{j=1}^{n}s(i,j)$
题解
先来个$O(n^2)$暴力看看有没有读错题(怕了
1 |
|
40pts到手(打爆力明题意)
点分正解?
考虑点分治,对于当前分治中心,统计出它自己出发到分治块内的所有路径对自己答案的贡献,和经过它的路径对当前分治块内点的贡献。自己出发到分治块内的所有路径对自己答案的贡献很好求,现在考虑怎么求经过它的路径对当前分治块内点的贡献。
我们对于当前分治中心的每一个子树分别考虑,令$cnt[i]$为从分治中心出发的进入其他子树的所有路径中,包含颜色$i$的路径条数,$size$为除了该子树外当前分治块内所有的点的个数。那么,我们dfs这棵子树计算贡献,假设当前dfs到$x$,首先给$sum_x$加上$\sum cnt[i]$,即所有在其他子树中出现的颜色的贡献总和,然后计算$x$到分治中心的路径上颜色的贡献。
对于一个出现在分治中心到$x$的路径上的颜色$c$,它对$x$的贡献为$size-cnt[c]$,因为$c$已经在一些路径上出现,它现在能产生的额外贡献为它原来没有出现的路径条数。所以我们给$sum_x$还要加上$size-cnt[c]$。同时$c$也会对$x$的子树内所有点产生贡献,所以这个贡献要像标记一样往下传递,然后标记一下$c$的贡献已经被计算过,往下dfs时就不用再次计算了。
所以只需要对于当前分治中心求出$cnt$数组和每棵子树的$size$,进入一棵子树时减去子树自己内部对$cnt$产生的贡献。同时为了防止复杂度退化,我们不能对于所有颜色求$cnt$,要先统计一下当前分治块内有哪些颜色出现了,这样枚举块内所有颜色的复杂度才是O(分治块大小)。
复杂度$O(nlogn)$
1 |
|
溜了溜了