P9847 [ICPC2021 Nanjing R] Crystalfly 题解 (SPJ)

[ICPC2021 Nanjing R] Crystalfly

传送门?

题面翻译

给定一个 n ( 1 ≤ n ≤ 1 0 5 ) n(1\le n\le10^5) n(1n105) 个节点的树,每个节点上有 a i a_i ai 只晶蝶。派蒙最初在 1 1 1 号节点,并获得 1 1 1 号节点的所有晶蝶,接下来每一秒她可以移动到相邻的节点上并获得节点上的所有晶蝶,但是当她每到达一个节点 u u u 后,对于每个与 u u u 相邻的节点 v v v,节点 v v v 上的的晶蝶会在 t v ( 1 ≤ t v ≤ 3 ) t_v(1\le t_v\le3) tv(1tv3) 秒内消失,在 t v t_v tv 秒后再到达节点 v v v 将无法获得节点上的晶蝶。现在需要你求出最多可以获得的晶蝶数。

题目描述

Paimon is catching crystalflies on a tree, which are a special kind of butterflies in Teyvat. A tree is a connected graph consisting of n n n vertices and ( n − 1 ) (n - 1) (n1) undirected edges.

There are initially a i a_i ai crystalflies on the i i i-th vertex. When Paimon reaches a vertex, she can catch all the remaining crystalflies on the vertex immediately. However, the crystalflies are timid. When Paimon reaches a vertex, all the crystalflies on the adjacent vertices will be disturbed. For the i i i-th vertex, if the crystalflies on the vertex are disturbed for the first time at the beginning of the t ′ t' t-th second, they will disappear at the end of the ( t ′ + t i ) (t' + t_{i}) (t+ti)-th second.

At the beginning of the 0 0 0-th second, Paimon reaches vertex 1 1 1 and stays there before the beginning of the 1 1 1-st second. Then at the beginning of each following second, she can choose one of the two operations:

  • Move to one of the adjacent vertices of her current vertex and stay there before the beginning of the next second (if the crystalflies in the destination will disappear at the end of that second she can still catch them).
  • Stay still in her current vertex before the beginning of the next second.

Calculate the maximum number of crystalflies Paimon can catch in 1 0 1 0 1 0 1 0 10 10^{10^{10^{10^{10}}}} 1010101010 seconds.

输入格式

There are multiple test cases. The first line of the input contains an integer T T T indicating the number of test cases. For each test case:

The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) indicating the number of vertices.

The second line contains n n n integers a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) where a i a_i ai is the number of crystalflies on the i i i-th vertex.

The third line contains n n n integers t 1 , t 2 , ⋯ , t n t_1, t_2, \cdots, t_n t1,t2,,tn ( 1 ≤ t i ≤ 3 1 \le t_i \le 3 1ti3) where t i t_i ti is the time before the crystalflies on the i i i-th vertex disappear after disturbed.

For the next ( n − 1 ) (n - 1) (n1) lines, the i i i-th line contains two integers u i u_i ui and v i v_i vi ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin) indicating an edge connecting vertices u i u_i ui and v i v_i vi in the tree.

It’s guaranteed that the sum of n n n of all the test cases will not exceed 1 0 6 10^6 106.

输出格式

For each test case output one line containing one integer indicating the maximum number of crystalflies Paimon can catch.

样例 #1

样例输入 #1

2
5
1 10 100 1000 10000
1 2 1 1 1
1 2
1 3
2 4
2 5
5
1 10 100 1000 10000
1 3 1 1 1
1 2
1 3
2 4
2 5

样例输出 #1

10101
10111

提示

For the first sample test case, follow the strategy below.

  • During the 0 0 0-th second
    • Paimon arrives at vertex 1 1 1;
    • Paimon catches 1 1 1 crystalfly;
    • Crystalflies in vertices 2 2 2 and 3 3 3 are disturbed.
  • During the 1 1 1-st second
    • Paimon arrives at vertex 3 3 3;
    • Paimon catches 100 100 100 crystalflies.
  • During the 2 2 2-nd second
    • Paimon arrives at vertex 1 1 1;
    • Crystalflies in vertex 2 2 2 disappears.
  • During the 3 3 3-rd second
    • Paimon arrives at vertex 2 2 2;
    • Crystalflies in vertices 4 4 4 and 5 5 5 are disturbed.
  • During the 4 4 4-th second
    • Paimon arrives at vertex 5 5 5;
    • Paimon catches 10000 10000 10000 crystalflies;
    • Crystalflies in vertex 4 4 4 disappears.

For the second sample test case, the optimal strategy is the same with the first sample test case. Crystalflies in vertex 2 2 2 are scheduled to disappear at the end of the 3 3 3-rd (instead of the 2 2 2-nd) second, allowing Paimon to catch them.

以上来自洛谷 以上来自洛谷 以上来自洛谷

解题思路

好好看题,就知道是树形 d p dp dp。定义 f u , 0 或 1 f_{u,0或1} fu,01 为遍历以 u u u 为根的整棵子树且 u u u 点的子节点的晶蝶消失( f u , 0 f_{u,0} fu,0)或不消失( f u , 1 f_{u,1} fu,1)的情况下所能获得的最大晶蝶数量。记与 u u u 相邻的非父亲节点中 t i = 3 t_i=3 ti=3 的节点晶蝶数量的最大值和第二大值分别为 m a x 1 , m a x 2 max1,max2 max1,max2,若不存在特判即可。
如果当前节点不存在 t i = 3 t_i=3 ti=3 的节点,则 f u , 0 = ( Σ f v , 1 ( v ∈ s o n u ) ) + m a x ( a v ) + f u , 1 = Σ f v , 1 ( v ∈ s o n u ) f_{u,0}=(\Sigma f_{v,1}(v\in son_u))+max(a_v)+f_{u,1}=\Sigma f_{v,1}(v\in son_u) fu,0=(Σfv,1(vsonu))+max(av)+fu,1=Σfv,1(vsonu)
如果当前节点存在 t i = 3 t_i=3 ti=3 的节点,那么通过手动画图观察发现,记所有子节点的 f v , 1 f_{v,1} fv,1 的和 Σ f v , 1 ( v ∈ s o n u ) \Sigma f_{v,1}(v\in son_u) Σfv,1(vsonu) s u m sum sum f u , 0 f_{u,0} fu,0 结果不变, f u , 1 = max ⁡ ⁡ ( f v , 0 + a v + s u m − f v , 1 + m a x 1 , f v , 0 + a v + s u m − f v , 1 + m a x 2 ) f_{u,1}=\max⁡(f_{v,0}+a_v+sum−f_{v,1}+max1,f_{v,0}+a_v+sum−f_{v,1}+max2) fu,1=max(fv,0+av+sumfv,1+max1,fv,0+av+sumfv,1+max2)
最后的答案为 f 1 , 1 + a 1 f_{1,1}+a_1 f1,1+a1​。

AC Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Maxn = 1e5 + 5;
vector<int> g[Maxn];
int n, u, v;
int a[Maxn];
int f[Maxn], sum[Maxn], t[Maxn];
inline void dfs(int u, int fa) {int maxx = 0;multiset<int> st;for (int v : g[u]) {if (v == fa) {continue;}dfs(v, u);sum[u] += f[v];maxx = max(maxx, a[v]);if (t[v] == 3) {st.insert(a[v]);}}f[u] = sum[u] + maxx;st.insert(LONG_LONG_MIN);for (int v : g[u]) {if (v == fa) {continue;}if (t[v] == 3) {st.erase(st.find(a[v]));}f[u] = max(f[u], sum[u] - f[v] + a[v] + sum[v] + *st.rbegin());if (t[v] == 3) {st.insert(a[v]);}}
}
inline void solve() {cin >> n;for (int i = 1; i <= n; i++) {g[i].clear();f[i] = sum[i] = 0;}for (int i = 1; i <= n; i++) {cin >> a[i];}for (int i = 1; i <= n; i++) {cin >> t[i];}for (int i = 1; i <= n - 1; i++) {cin >> u >> v;g[u].push_back(v);g[v].push_back(u);}dfs(1, 0);cout << f[1] + a[1] << endl;
}
inline void work() {int T;cin >> T;while (T--) {solve();}
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/629108.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NetSuite学习笔记 - 中心

一、什么是中心&#xff1f; 对于每个用户&#xff0c;NetSuite 会根据用户的指定角色显示一组可变的标签页面&#xff0c;称为中心。通俗来讲呢&#xff0c;NetSuite的中心其实就是我们常说的“导航菜单”。 只是在我过去常见的系统中&#xff0c;导航菜单一般都是固定的&am…

Elasticsearch Windows部署-ELK技术栈

1、下载Elasticsearch、kibana、logstash 本文不介绍ELK相关原理知识&#xff0c;只记录部署操作过程 下载地址Past Releases of Elastic Stack Software | Elastic 选择同一版本&#xff0c;这里选择是当前最新版本8.11.3 解压放在同目录下&#xff0c;方便后续操作与使用 …

浅入浅出Spring架构设计

浅入浅出Spring架构设计 前言 为什么需要Spring? 什么是Spring? 对于这样的问题&#xff0c;大部分人都是处于一种朦朦胧胧的状态&#xff0c;说的出来&#xff0c;但又不是完全说的出来&#xff0c;今天我们就以架构设计的角度尝试解开Spring的神秘面纱。 本篇文章以由浅…

RK3566 linux加入uvc app

SDK中external/uvc_app/目录提供了将板卡模拟成uvc camera的功能。 一、buildroot使能uvc_app 1、进入到buildroot目录 在sdk目录下执行以下命令&#xff1a; cd buildroot 2、选择defconfig 执行命令&#xff1a; source build/envsetup.sh 输入数字然后回车选择板卡&…

两数之和(Hash表)[简单]

优质博文&#xff1a;IT-BLOG-CN 一、题目 给定一个整数数组nums和一个整数目标值target&#xff0c;请你在该数组中找出"和"为目标值target的那两个整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元…

工具推荐 |Devv.ai — 最懂程序员的新一代 AI 搜索引擎

介绍 伴随 GPT 的出现&#xff0c;我们可以看到越来越多的 AI 产品&#xff0c;其中也不乏针对程序员做的代码生成工具。 今天介绍的这款产品是一款针对中文开发者的 AI 搜索引擎&#xff0c;Devv.ai 使用 Devv.ai 的使用非常简单&#xff0c;就是传统的搜索场景&#xff…

JVM:类加载机制

JVM&#xff1a;类加载机制 1. 什么是类加载机制2. 类加载的过程(生命周期)2.1 加载2.2 校验&#xff08;验证&#xff09;2.3 准备2.n 六种情况对类进行“初始化” 1. 什么是类加载机制 Java虚拟机把描述类的数据从Class文件加载到内存&#xff0c;并对数据进行校验、转换解析…

数字化赋能TPM:开启智能生产新篇章

随着科技的快速发展&#xff0c;数字化已经成为企业提升生产效率、优化管理的重要手段。对于设备密集型的制造业而言&#xff0c;传统的TPM&#xff08;全员生产维护&#xff09;方式已经难以满足现代生产的需求。如何将数字化技术与TPM相结合&#xff0c;赋能TPM&#xff0c;提…

机器学习:线性回归模型的原理、应用及优缺点

一、原理 线性回归是一种统计学和机器学习中常用的方法&#xff0c;用于建立变量之间线性关系的模型。其原理基于假设因变量&#xff08;或响应变量&#xff09;与自变量之间存在线性关系。 下面是线性回归模型的基本原理&#xff1a; 模型拟合&#xff1a; 通过最小二乘法&…

2024华数杯国际数学建模B题思路+代码+模型+论文

2024华数杯国际数学建模B题思路代码模型论文&#xff1a;1.17上午第一时间更新&#xff0c;详细内容见文末名片 问题B&#xff1a;光伏电 背景 中国的电力构成包括传统的能源发电&#xff08;如煤炭、石油和天然气&#xff09;、可再生能源发电 &#xff08;如水力发电、风能…

SpringMVC 文件上传和下载

文章目录 1、文件下载2、文件上传3. 应用 Spring MVC 提供了简单而强大的文件上传和下载功能。 下面是对两者的简要介绍&#xff1a; 文件上传&#xff1a; 在Spring MVC中进行文件上传的步骤如下&#xff1a; 在表单中设置 enctype“multipart/form-data”&#xff0c;这样…

Linux网络文件共享服务之NFS

目录 一、NFS简介 1、NFS协议 2、NFS存储 3、NFS原理 4、NFS相关软件介绍 5、NFS配置文件 二、exportfs和showmount命令 三、搭建NFS服务器 1、搭建过程 ​2、客户端权限问题 2.1 权限参数说明 2.2 配置客户端的读写权限 2.3 创建文件的属主和属组权限 2.4 客户端…

供应链|库存定位的高效策略:如何巧妙调换安全库存换取服务速度?

论文作者&#xff1a;Hanzhang Qin, David Simchi-Levi, Ryan Ferer, Jonathan Mays, Ken Merriam, Megan Forrester, Alex Hamrick 论文解读者&#xff1a;马玺渊 王艺桦 编者按 本次解读的文章发表于 Production and Operations Management&#xff0c;原文信息&#xff1a;…

vs2022配置OpenCV测试

1&#xff0c;下载Opencv安装包 OpenCV官网下载地址&#xff1a;Releases - OpenCV 大家可以按需选择版本进行下载&#xff0c;官网下载速度还是比较慢的&#xff0c;推荐大家使用迅雷进行下载 下载安装包到自定义文件夹下 双击安装 按以下图示进行安装 2、 添加环境变量 打…

Servlet项目教学(附实例代码)

【员工信息管理】 1.员工信息管理 1.1 介绍 用户进行登录后,可以对员工信息进行管理(增删查改),等操作.如果用户没有登录,不能访问员工操作页面.并且员工操作页面显示当前登录用户信息. 1.2 技术点 使用VueElementUI充当前端界面,使用ServletJDBCMysql提供数据管理控制.后端统…

二次开发在线预约上门服务、预约到家系统 增加开发票功能 轮播图链接跳转 uniapp代码

客户具体要求&#xff1a; 1、在我的个人中心里面增加一个 开票功能&#xff0c;点击进去之后可以查看到能开票的订单列表&#xff0c;如果是个人是填写姓名电话邮箱&#xff0c;就是填写单位名称 税号 邮箱&#xff0c;提交申请到后台审核&#xff0c;如果审核通过后线下人工…

各种设备上恢复已删除的文件和文件夹的数据恢复软件清单

最好的数据恢复软件可以简单轻松地恢复计算机、移动设备或存储介质上已删除的文件和文件夹。 询问任何经历过数据丢失的人这是否是一种有趣的经历&#xff0c;他们会告诉您数据丢失&#xff0c;无论是由于硬件或软件故障、意外删除还是网络犯罪&#xff0c;都会带来极大的压力…

高精度算法笔记

目录 加法 减法 乘法 除法 高精度加法的步骤&#xff1a; 1.高精度数字利用字符串读入 2.把字符串翻转存入两个整型数组A、B 3.从低位到高位&#xff0c;逐位求和&#xff0c;进位&#xff0c;存余 4.把数组C从高位到低位依次输出 1.2为准备 vector<int> A, B, C…

接近8000字的SpringSpring常用注解总结!安排

接近8000字的Spring/Spring常用注解总结&#xff01;安排 为什么要写这篇文章&#xff1f; 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多&#xff0c;我看了文章内容之后属实觉得质量有点低&#xff0c;并且有点会误导没有太多实际使用经验的人&#xff…

如何用AI提高论文阅读效率?

已经2024年了&#xff0c;该出现一个写论文解读AI Agent了。 大家肯定也在经常刷论文吧。 但真正尝试过用GPT去刷论文、写论文解读的小伙伴&#xff0c;一定深有体验——费劲。其他agents也没有能搞定的&#xff0c;今天我发现了一个超级厉害的写论文解读的agent &#xff0c…