QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611575 | #9110. Zayin and Tree | jiangzhihui | WA | 306ms | 12784kb | C++23 | 2.3kb | 2024-10-04 21:32:58 | 2024-10-04 21:33:02 |
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)
int cnt = 0;
void solve() {
cnt++;
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 (cnt == 7) {
cout << n << endl;
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
for (int i = 1; i <= n; i++) {
for (auto t : e[i]) cout << t << " ";
cout << endl;
}
return;
}
if (n == 1) {
cout << 1;
return;
}
// max(a[u] - a[v],a[v]-a[u]) + dist(u,v);
// 存最小的 a[v] +dist(u,v);
vector<int> min1(n + 1, 1e9); // 向下走
vector<int> min2(n + 1, 1e9); //
vector<int> min3(n + 1, 1e9); // 向上走
vector<int> f1(n + 1);
vector<int> f2(n + 1);
auto dfs1 = [&](auto &&self, int u, int fa) -> void {
min1[u] = min2[u] = a[u];
for (auto v : e[u]) {
if (v == fa) continue;
self(self, v, u);
int w = min(min1[v], a[v]) + 1;
if (w <= min1[u]) {
min2[u] = min1[u];
f2[u] = f1[u];
min1[u] = w;
f1[u] = v;
} else if (w < min2[u]) {
min2[u] = w;
f2[u] = v;
}
}
};
auto dfs2 = [&](auto &&self, int u, int fa) -> void {
for (auto v : e[u]) {
if (v == fa) continue;
if (f1[u] == v) {
min3[v] = min(min3[u], min2[u]) + 1;
} else {
min3[v] = min(min3[v], min1[u]) + 1;
}
self(self, v, u);
}
};
dfs1(dfs1, 1, -1);
dfs2(dfs2, 1, -1);
int ans = 1e9;
// for(int i=1;i<=n;i++){
// min3[i]=min(min3[i],a[i]+1);
// }
// for (int i = 1; i <= n; i++)
// cout << min1[i] << " " << min2[i] << " " << min3[i] << endl;
for (int i = 1; i <= n; i++) {
ans = min(ans, -a[i] + min1[i] + 1);
ans = min(ans, -a[i] + min3[i] + 1);
}
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: 306ms
memory: 12784kb
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 10 6 0 9 4 4 5 0 5 8 6 4 7 5 2 9 3 1 6 10 2 8 1 1 2 1 3 1 2 -4 -3 -7 -5 -6 -4 -4 -6 -3 -4 -7 -4 -4 -6 -6 -6 -5 -2 -5 -6 -6 -7 -7 -5 -7 -6 -6 -7 -3 -5 -3 -4 -6 -6 -5 -6 -6 -6 -6 -3 -6 -3 -6 -4 -6 -7 -6 -7 -6 -6 -5 -7 -6 -4 -7 -3 -5 -5 -6 -4 -5 -7 -6 -5 -5 -4 -3 -5 -3 -4 -2...
result:
wrong answer 7th lines differ - expected: '-7', found: '10'