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
思路:
树上启发式合并
从根节点出发到每个位置的每个字符的奇偶性记为每个位置的状态,每次统计一下每个状态的最大深度
为了保证链经过当前节点u,我们先计算每个子树的答案,再更新子树状态对深度的贡献。
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r //#define mp make_pair #define pb push_back #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdi pair<double, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head inline int read() {int a = 1, b = 0;char ch = getchar();while(ch < '0' || ch > '9') {if(ch == '-') a = -1;ch = getchar();}while('0' <= ch && ch <= '9') {b = b*10 + ch-'0';ch = getchar();}return a*b; } const int N = 5e5 + 5, M = 5e6 + 5; const int INF = 1e8; vector<pii> g[N]; int n, p, dp[N], sz[N], son[N], deep[N], st[N], mx[M]; char c[2]; void get_son(int u, int o) {sz[u] = 1;deep[u] = deep[o] + 1;for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;int w = g[u][i].se;st[v] = st[u] ^ (1<<w);get_son(v, u);if(sz[v] > sz[son[u]]) son[u] = v;sz[u] += sz[v];} } void CAL(int p, int u) {if(mx[st[u]] >= 0) dp[p] = max(dp[p], mx[st[u]]+deep[u]-2*deep[p]);for (int i = 0; i < 22; ++i) {if(mx[st[u]^(1<<i)] >= 0) dp[p] = max(dp[p], mx[st[u]^(1<<i)]+ deep[u]-2*deep[p]);}for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;CAL(p, v);} } void ADD(int u) {mx[st[u]] = max(mx[st[u]], deep[u]);for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;ADD(v);} } void DELETE(int u) {if(mx[st[u]] >= 0) mx[st[u]] = -INF;for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;DELETE(v);} } void dfs(int u) {for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;if(v != son[u]) {dfs(v);DELETE(v);}}if(son[u]) dfs(son[u]);if(mx[st[u]] >= 0) dp[u] = mx[st[u]] - deep[u];for (int i = 0; i < 22; ++i) {if(mx[st[u]^(1<<i)] >= 0) dp[u] = max(dp[u], mx[st[u]^(1<<i)] - deep[u]);}mx[st[u]] = max(mx[st[u]], deep[u]);for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;if(v != son[u]) {CAL(u, v);ADD(v);}}for (int i = 0; i < g[u].size(); ++i) {int v = g[u][i].fi;dp[u] = max(dp[u], dp[v]);} } int main() {n = read();for (int i = 2; i <= n; ++i) {p = read();scanf("%s", c);g[p].pb({i, c[0]-'a'});}get_son(1, 0);for (int i = 0; i < M; ++i) mx[i] = -INF;dfs(1);for (int i = 1; i <= n; ++i) printf("%d%c", dp[i], " \n"[i==n]);return 0; }
转载于:https://www.cnblogs.com/widsom/p/10773406.html
Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths相关推荐
- 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包
B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- [CF741D] Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
问题描述 Just in case somebody missed it: we have wonderful girls in Arpa's land. Arpa has a rooted tree ...
- Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...
- 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的 ...
- 【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 [题解]这 ...
- 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
- Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...
- Codeforces Round #168 (Div. 2)D. Zero Tree(DP,中等难度)
D. Zero Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
最新文章
- 02.Apollo配置中心整合spring cloud zuul
- VisualStudio2013 如何打开之前版本开发的(.vdproj )安装项目
- python基础语法合集-Python基础语法(四)—列表、元组、字典、集合、字符串
- nginx通过用户和密码来实现认证功能
- STC89C52 STC89LE52 NRF24L01无线 教程 (一)
- 全自动安装 linux光盘,CentOS 7.1全自动安装光盘制作详解
- ToPILImage
- linux下configure命令详细介绍[转]
- 不×××,用google!!
- ntpdate从指定服务器同步时间,ntpdate:设置服务器时间定期同步
- HTTP 的概念、原理、工作机制、数据格式和REST(HenCoder学习总结,待整理中...)
- 自我理解:封装、继承和多态
- 高效能人士的七个习惯读后感与总结概括-(第三章)
- php 相似文章,php 比较两篇文章的相似度的方法
- 从零开始搭建一个Vue项目
- 电容倍增器作为电源滤波器
- 说说wps jsa的ListBox控件的数组写入方法
- android 和RxJava配合使用的两个图片压缩框架LuBan、Compressor
- 【从0到1搭建LoRa物联网】9、国产LoRa终端ASR6505 PingPong通信OLED显示
- VC++ MFC资源中添加PNG,JPG等图片资源
热门文章
- Content-Disposition 响应头,设置文件在浏览器打开还是下载
- jquery关于多个显示隐藏
- ASP.NET Zero--5.配置权限
- hive日志位置(日志定位报错:Failed with exception Unable to move sourcehdfs://namenode/tmp/hive-pmp_bi/h)...
- 可爱的 Python: 使用 mechanize 和 Beautiful Soup 轻松收集 Web 数据
- 使用说明 思迅收银系统_便利店收银使用的收银系统应该取决于什么?
- 获取ClassLoader的途径
- lambda中使用filter过滤
- fastq质量值_fastq 数据格式解析
- 数据库:推荐几款 Redis 可视化工具,你都用过吗?