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

2024-01-17 19:20

本文主要是介绍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;
}

这篇关于P9847 [ICPC2021 Nanjing R] Crystalfly 题解 (SPJ)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/616960

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?

【最新华为OD机试E卷-支持在线评测】机器人活动区域(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,支持题目在线评测,专栏文章质量平均 94 分 最新华为OD机试目录: https://blog.

2023 CCPC(秦皇岛)现场(第二届环球杯.第 2 阶段:秦皇岛)部分题解

所有题目链接:Dashboard - The 2023 CCPC (Qinhuangdao) Onsite (The 2nd Universal Cup. Stage 9: Qinhuangdao) - Codeforces 中文题面: contest-37054-zh.pdf (codeforces.com) G. Path 链接: Problem - G - Codeforces