-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxOutput.cpp
More file actions
34 lines (32 loc) · 1.42 KB
/
maxOutput.cpp
File metadata and controls
34 lines (32 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
long long maxOutput(int n, vector<vector<int>> &edges, vector<int> &price) {
vector<vector<int>> g(n);
for (auto &e : edges) {
int x = e[0], y = e[1];
g[x].push_back(y);
g[y].push_back(x); // 建树
}
long ans = 0;
// 返回带叶子的最大路径和,不带叶子的最大路径和
function<pair<long, long>(int, int)> dfs = [&](int x, int fa) -> pair<long, long> {
long p = price[x], max_s1 = p, max_s2 = 0;
for (int y : g[x])
if (y != fa) {
auto[s1, s2] = dfs(y, x);
// 前面最大带叶子的路径和 + 当前不带叶子的路径和
// 前面最大不带叶子的路径和 + 当前带叶子的路径和
ans = max(ans, max(max_s1 + s2, max_s2 + s1));
max_s1 = max(max_s1, s1 + p);
max_s2 = max(max_s2, s2 + p); // 这里加上 p 是因为 x 必然不是叶子
}
return {max_s1, max_s2};
};
dfs(0, -1);
return ans;
}
};
// 作者:灵茶山艾府
// 链接:https://leetcode.cn/problems/difference-between-maximum-and-minimum-price-sum/solutions/2062782/by-endlesscheng-5l70/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。