QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#668154 | #6437. Paimon's Tree | Kdlyh | RE | 0ms | 0kb | C++20 | 1.5kb | 2024-10-23 11:59:17 | 2024-10-23 11:59:17 |
answer
#include<bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...) 42
#endif
using i64 = long long;
namespace ranges = std::ranges;
//ti = 3, 可以多选一个旁支
//ti < 3, 只能选一条路
void solve()
{
#define tests
int n; std::cin >> n; std::vector<int> a(n); std::vector<int> t(n); for (auto& ai : a) {std::cin >> ai;} for (auto& ti : t) {std::cin >> ti;}
std::vector adj(n, std::vector<int>()); for (int i = 0, u, v; i + 1 < n; i++) {std::cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u);}
std::vector<i64> dp(n);
auto dfs = [&](auto self, int now, int fa) -> void {
std::set<std::pair<int, int>> S; for (auto& to : adj[now]) if (to != fa) {S.emplace(a[to], to);}
auto get_max_except = [&](int id) {auto it{S.rbegin()}; if (it->second == id) {it++;} return it->first;};
for (auto& to : adj[now]) if (to != fa) {
self(self, to, now);
if (t[to] < 3) {
dp[now] = std::max(dp[now], dp[to] + a[to]);
dp[now] = std::max(dp[now], dp[to] + get_max_except(to));
} else {
dp[now] = std::max(dp[now], dp[to] + a[to] + get_max_except(to));
}
}
};
dfs(dfs, 0, -1);
std::cout << dp[0] + 1 << "\n";
}
signed main()
{
std::cin.tie(nullptr)->sync_with_stdio(false);
int _(1);
#ifdef tests
std::cin >> _;
#endif
while(_--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 5 1 7 3 5 4 1 3 2 3 3 4 4 5 4 6 1 1000000000 1 2