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

http://codeforces.com/problemset/problem/741/D

题意:

  一棵根为1 的树,每条边上有一个字符(a-v共22种)。 求每个子树内的最长的路径,使得路径上的边按照一定顺序排列后是回文串。

分析:

  dsu on tree。询问子树信息。

  首先将这22个字符,转化为二进制。a=1,b=10,c=100...如果一条路径可以是回文串,那么要求路径的异或和最多有一个1。所以记录下从根到每个点的异或和,那么一条路径的异或和就是dis[u]^dis[v],lca上面的异或后消失了。

  之后维护每个子树内异或和为x的最大深度是多少。记为f。

  更新答案:按照点分治的思想,先更新不经过根的,然后求出经过根的(更新分成两步,第一步更新答案,第二步更新f数组。防止更新了在子树内部的)。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int head[N], nxt[N], to[N], len[N], En;
int fa[N], siz[N], son[N], dis[N], deth[N], ans[N];
int f[( << ) + ];
int Mx, D; void add_edge(int u,int v,int w) {
++En; to[En] = v; len[En] = w; nxt[En] = head[u]; head[u] = En;
} void dfs(int u) {
siz[u] = ;
deth[u] = deth[fa[u]] + ;
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
fa[v] = u;
dis[v] = dis[u] ^ ( << len[i]);
dfs(v);
siz[u] += siz[v];
if (!son[u] || siz[son[u]] < siz[v]) son[u] = v;
}
} void add(int u) {
if (f[dis[u]]) Mx = max(Mx, deth[u] + f[dis[u]] - D); // 异或后为0
for (int i=; i<; ++i) // 异或后有一个1
if (f[( << i) ^ dis[u]]) Mx = max(Mx, deth[u] + f[( << i) ^ dis[u]] - D);
}
void Calc(int u) { // 计算答案
add(u);
for (int i=head[u]; i; i=nxt[i]) Calc(to[i]);
}
void upd(int u) {
f[dis[u]] = max(deth[u], f[dis[u]]);
}
void update(int u) { // 更新f数组
upd(u);
for (int i=head[u]; i; i=nxt[i]) update(to[i]);
}
void Clear(int u) {
f[dis[u]] = ;
for (int i=head[u]; i; i=nxt[i]) Clear(to[i]);
} void solve(int u,bool c) {
for (int i=head[u]; i; i=nxt[i])
if (to[i] != son[u]) solve(to[i], );
if (son[u]) solve(son[u], ); D = deth[u] * ;
for (int i=head[u]; i; i=nxt[i]) Mx = max(Mx, ans[to[i]]); //不经过根的
for (int i=head[u]; i; i=nxt[i])
if (to[i] != son[u]) Calc(to[i]), update(to[i]); // update(u) !!! 先更新答案,在更新f数组,(防止计算到在子树内部的路径)
add(u); upd(u);
ans[u] = Mx;
if (!c) Clear(u), Mx = ;
} int main() {
int n = read();
char c[];
for (int i=; i<=n; ++i) {
int u = read(); scanf("%s", c);
add_edge(u, i, c[] - 'a');
}
dfs();
solve(, );
for (int i=; i<=n; ++i) printf("%d ",ans[i]);
return ;
}

CF 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths的更多相关文章

  1. 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,求每个子树中最长的边,满 ...

  2. 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 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  3. 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的 ...

  4. 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 ...

  5. 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  6. 【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 [题解]这 ...

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

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

  8. [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree

    题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...

  9. Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...

随机推荐

  1. 谈谈hashcode和equals的用法

    HASH: Hash是散列的意思,就是把任意长度的输入,通过散列算法变换成固定长度的输出,该输出就是散列值.关于散列值,有以下几个关键结论: 1.如果散列表中存在和散列原始输入K相等的记录,那么K必定 ...

  2. mongodb在windows平台安装和启动

    mongodb 官网:https://www.mongodb.com mongodb 官网下载: mongodb-win32-x86_64-2008plus-ssl-3.4.2-signed.msi ...

  3. 手把手带你理解style

    在写代码的时候,经常遇到自定义的style,有的用来设置属性,有的用来设置主题,搞的自己云里雾里,因此在心底暗暗发誓,等到空闲的时候,一定好好学学android中的style的究竟是个什么东西,到底有 ...

  4. codeforces 814D An overnight dance in discotheque

    题目链接 正解:贪心. 首先我们可以计算出每个圆被多少个圆覆盖. 很显然,最外面的圆是肯定要加上的. 然后第二层的圆也是要加上的.那么第三层就不可能被加上了.同理,第四层的圆又一定会被加上. 然后我们 ...

  5. 「Newcoder练习赛40D」小A与最大子段和

    题目 挺好的一道题 我们考虑把\(i\)作为选取的最大子段的结束位置,我们如何往前计算贡献呢 考虑一下这个乘上其在队列中的位置可以表示为这个数被算了多少次,而我们往前扩展一位当前已经被扩展的就会被计算 ...

  6. eclipse 设置字体大小

    步骤: 1.打开eclipse,在工具栏里找到 Window -> Perferences,打开如下图: 2.展开General -> Appearance -> Colors an ...

  7. 二十二、详述 IntelliJ IDEA 中恢复代码的方法

    在咱们正常开发项目的时候,难免遇到在开发过程中由于某种原因,想要将代码恢复到前一版本的情景.特别是在咱们删除了某些代码,想要恢复之前删除的代码的时候,了解这个在 IntelliJ IDEA 中恢复代码 ...

  8. Gradle Goodness: Running Java Applications from External Dependency

    With Gradle we can execute Java applications using the JavaExec task or the javaexec() method. If we ...

  9. Notes 20180307 : 运算符

    我们前边曾说过程序=数据结构+算法,数据结构讲的是数据在内存中的存储形式,这个我会作为2018的一个重点来研究,不过在这里不做赘述,前半年的工作以JavaSE为主.算法则是我们在数据结构的基础上对其的 ...

  10. Oracle子查询之高级子查询

    Oracle 高级子查询 高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据. 1,多列子查询 主查询与子查询返回的多个列进行比较 查询与141号或174号员工的manager_id ...