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.hqwc.cn/news/414942.html

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

相关文章

铝壳电阻在哪些领域和应用中常见?

铝壳电阻是一种常见的电子元件&#xff0c;广泛应用于各种领域和应用场景。以下是铝壳电阻在各个领域和应用中的常见用途&#xff1a; 电力系统&#xff1a;铝壳电阻在电力系统中主要用于限制电流、分压和负载平衡。它们通常安装在变压器、发电机、电动机等设备中&#xff0c;以…

【特征工程】分类变量:简洁而高效的频数编码(Frequency Encoding)

频数编码&#xff08;Frequency Encoding&#xff09;&#xff1a;一个简洁而高效的特征编码方法 1. 频数编码是什么&#xff1f; 频数编码的主要思想是用每个类别在数据集中出现的频率来替代原始的类别标签。这样可以用一个单一的数值来代表每个类别&#xff0c;而不引入高维…

2024-01-19(SpringCloudThreadLocal)

1.Seata的TC服务注册到Nacos注册中心当中 2.Seata为我们提供了AT&#xff0c;TCC&#xff0c;SAGA&#xff0c;XA事务解决方案。 3.XA规范是一种分布式事务处理标准&#xff0c;XA规范描述了全局的TM与局部的RM之间的接口&#xff0c;几乎所有的主流的数据库都对XA规范提供了…

innoDB存储引擎

1.逻辑存储结构 行数据->行->页->区->段->表空间 表空间(ibd文件)&#xff0c;一个mysql实例可以对应多个表空间&#xff0c;来存储记录&#xff0c;索引等数据。 段&#xff1a;分为数据段和索引段&#xff0c;回滚段&#xff0c;数据段就是B树的叶子节点&am…

红队渗透靶机:TOPPO: 1

目录 信息收集 1、arp 2、nmap 3、nikto 4、whatweb 5、dirsearch WEB tips1 tips2 SSH登录 提权 系统信息收集 本地 信息收集 1、arp ┌──(root㉿ru)-[~/kali] └─# arp-scan -l Interface: eth0, type: EN10MB, MAC: 00:0c:29:69:c7:bf, IPv4: 192.168.110…

Python连接数据库的梳理

我们通常用的数据库类型主要有关系型数据库&#xff0c;非关系型数据库等&#xff0c;其中关系型数据库主要有Microsoft SQL Server ,MySQL,Oracle&#xff0c;SQLite等&#xff0c;常用的非关系型数据库包括Redis、DynamoDB&#xff0c;MongoDB等 ​​​​​​​ 一 关系型…

策略路由与NQA联动示例

某公司网络使用SwitchA做汇聚层交换机&#xff0c;接入层交换机LSW做用户网关&#xff0c;LSW和SwitchA之间路由可达。汇聚层交换机SwitchA通过两条链路连接到两个核心交换机上&#xff0c;一条是高速链路&#xff0c;网关为10.1.20.1/24&#xff1b;另外一条是低速链路&#x…

2024 年 8 款最好的免费 OCR 软件

2024 年 8 款最佳 OCR 软件通过使用最好的 OCR 软件&#xff0c;您可以快速扫描文章、表格和其他文本&#xff0c;并将它们保存为 PDF 格式。OCR 代表光学字符识别&#xff0c;因此当您需要优化工作流程并通过将发票、报告和其他办公室文档转换为更易于存储和与同事共享的数字文…

AI新工具(20240119):Coach 微软推出独立 AI 工具辅助提高学习者阅读能力,Tweetify长文转换

Reading Coach-微软推出独立 AI 工具辅助提高学习者阅读能力 微软的Reading Coach是一款面向学生群体的生成式AI工具&#xff0c;旨在通过个性化和有吸引力的练习帮助学习者提高阅读能力。用户只需要登录微软账号&#xff0c;就能在课堂或者家中免费使用这款工具。Reading Coa…

Oracle命令大全

文章目录 1. SQL*Plus命令&#xff08;用于连接与管理Oracle数据库&#xff09;2. SQL数据定义语言&#xff08;DDL&#xff09;命令3. SQL数据操作语言&#xff08;DML&#xff09;命令4. PL/SQL程序块5. 系统用户管理6. 数据备份与恢复相关命令1. SQL*Plus命令&#xff08;用…

JAVA SECS发送Report C#处理SECS Report SECS发送事件资料大全 S6F11 建立通讯S1F13

发送S6F11非常简单&#xff0c;只需5~6行代码&#xff0c;最核心是代码清晰易懂。 任何人都可以一看就能上手&#xff0c;如果说用代码可读性作为不可替代性的壁垒就无话可说了。 private void buttonS6F11_Click(object sender, EventArgs e) {int nTransaction 0;// 数据部…