QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#609980 | #9110. Zayin and Tree | jiangzhihui | WA | 233ms | 18196kb | C++23 | 1.7kb | 2024-10-04 14:38:24 | 2024-10-04 14:38:25 |
Judging History
answer
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include <bits/Bingbong.h>
#endif
using namespace std;
#define i64 long long
#define pi acos(-1)
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
vector<vector<int>> e(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
if (n == 1) {
cout << 1;
return;
}
int ans = 2e9;
vector<vector<int>> dp(n + 1, vector<int>(3, 2e9));
// 在结点 i 为根的子树下选择了 j 个点的最小和
// a[v]+dist(u,v)
auto dfs = [&](auto &&self, int u, int fa) -> void {
dp[u][1] = a[u];
vector<int> vv;
for (auto v : e[u]) {
if (v == fa) continue;
self(self, v, u);
dp[u][1] = min(dp[u][1], dp[v][1] + 1); // 这个+1是当前结点
// 先考虑当前结点和子树中的某个点搭配
dp[u][2] = min(dp[u][2], dp[v][1] + 1 - a[u]);
dp[u][2] = min(dp[u][2], a[u] - dp[v][1] + 2);
// 再考虑子树中两点结合
vv.push_back(dp[v][1]);
}
sort(vv.begin(), vv.end(), [](int a, int b) { return a > b; });
if (vv.size() >= 2) {
dp[u][2] = min(dp[u][2], vv[0] - vv[1] + 1);
dp[u][2] = min(dp[u][2], -vv[0] + vv[1] + 2);
}
dp[u][1]++;
ans = min(ans, dp[u][2]);
};
dfs(dfs, 1, 0);
// for (int i = 1; i <= n; i++) cout << dp[i][1] << " " << dp[i][2] << endl;
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
/*
2
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 233ms
memory: 18196kb
input:
3009 5 4 5 3 4 2 1 2 2 3 3 4 3 5 5 4 4 1 1 2 1 2 2 3 3 4 3 5 10 5 8 1 0 8 7 5 2 0 4 2 4 3 8 3 9 1 2 1 3 3 6 4 5 5 7 6 10 10 6 8 8 4 8 0 6 6 0 2 7 10 1 7 2 9 2 3 3 4 1 5 1 6 6 8 1 2 10 9 0 4 0 4 6 0 2 0 0 1 5 1 3 1 7 2 6 1 2 1 9 1 4 5 8 7 10 10 8 8 1 2 7 4 8 6 0 8 1 6 1 7 1 5 7 9 1 3 1 2 2 10 3 4 1 8...
output:
0 -1 -6 -6 -7 -6 -6 -3 -3 -8 -6 -6 -3 -4 -7 -4 -5 -8 -5 -5 -5 -7 -3 -5 -3 -6 -6 -7 -7 -7 -4 -7 -6 -7 -7 -4 -6 -4 -4 -6 -7 -5 -7 -4 -7 -6 -4 -7 -3 -6 -5 -6 -8 -6 -7 -5 -6 -5 -8 -6 -4 -7 -2 -3 -6 -7 -3 -6 -7 -6 -5 -5 -5 -3 -4 -3 -4 -3 -7 -6 -8 -5 -6 -4 -7 -8 -3 -7 -5 -4 -6 -6 -5 -6 -3 -4 -8 -7 -8 -6 -...
result:
wrong answer 7th lines differ - expected: '-7', found: '-6'