QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#515612 | #6438. Crystalfly | yxyxyx | WA | 0ms | 3616kb | C++20 | 2.6kb | 2024-08-11 19:18:58 | 2024-08-11 19:18:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define scanf scanf_s
#define umap unordered_map
#define lowbit(x) (x&(-x))
#define ls (p<<1)
#define rs (p<<1|1)
//#define int long long
//---------------------
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll INF = 1e18 + 10;
const int inf = 1e9 + 10;
int mod = 998244353;
//---------------------
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1)ans = ans * a % mod; a = a * a % mod; b >>= 1; }return ans; }
ll inv(ll x) { return qpow(x % mod, mod - 2); }
int d4[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
int d8[8][2] = { {0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
void solve() {
int n; cin >> n;
vector<ll>a(n + 1);
vector<int>t(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)cin >> t[i];
vector<vector<int>>g(n + 1, vector<int>());
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<ll>>dp(n + 1, vector<ll>(3));
auto dfs = [&](auto slef, int u, int fa)->void {
dp[u][2] = a[u];
ll mx = 0, s = 0;
int mx1 = -1, mx2 = -1;
for (int v : g[u]) {
if (v == fa)continue;
slef(slef, v, u);
s += dp[v][1];
mx = max(mx, a[v]);
if (t[v] == 3) {
if (mx1 == -1)mx1 = v;
else {
if (a[v] > a[mx1]) {
mx2 = mx1;
mx1 = v;
}
else if (mx2 == -1)mx2 = v;
else if (a[v] > a[mx2])mx2 = v;
}
}
}
dp[u][1] = s + mx;
dp[u][2] += s;
for (int v : g[u]) {
if (v == fa)continue;
ll tmp = dp[v][2] + s - dp[v][1];
if (v == mx1 && mx2 != -1)tmp += a[mx2];
else if(mx1 != -1) tmp += a[mx1];
dp[u][1] = max(dp[u][1], tmp);
}
dp[u][0] = dp[u][1] + a[u];
};
dfs(dfs, 1, 0);
cout << dp[1][0] << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cout << fixed << setprecision(12);
//euler();
int t = 1; cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
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
output:
10101 10111
result:
ok 2 number(s): "10101 10111"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3616kb
input:
10 6 8 1 1 5 8 9 2 1 2 2 2 2 1 2 2 3 2 4 1 5 2 6 6 6 4 4 1 3 6 2 1 3 3 3 3 1 2 1 3 3 4 4 5 5 6 6 10 5 1 8 5 1 1 3 1 2 2 2 1 2 2 3 2 4 2 5 3 6 10 6 8 8 9 6 9 5 6 6 4 2 1 3 3 2 2 2 2 3 1 1 2 1 3 3 4 4 5 5 6 4 7 2 8 7 9 9 10 7 10 9 1 5 7 5 4 1 1 1 2 1 3 2 1 2 1 3 3 4 3 5 5 6 1 7 5 7 1 1 4 2 3 1 3 2 2 1...
output:
25 30 24 61 36 14 17 28 19 19
result:
wrong answer 2nd numbers differ - expected: '24', found: '30'