QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611540#9110. Zayin and TreejiangzhihuiWA 223ms12496kbC++231.8kb2024-10-04 21:22:522024-10-04 21:22:55

Judging History

你现在查看的是最新测评结果

  • [2024-10-04 21:22:55]
  • 评测
  • 测评结果:WA
  • 用时:223ms
  • 内存:12496kb
  • [2024-10-04 21:22:52]
  • 提交

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;
  }
  // 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 {
    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++) {
    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: 223ms
memory: 12496kb

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
-5
-3
-3
-6
-5
-6
-4
-4
-5
-2
-4
-6
-3
-3
-5
-4
-6
-5
-2
-5
-6
-4
-6
-7
-5
-7
-6
-6
-6
-5
-5
-4
-4
-6
-6
-5
-5
-6
-3
-6
-3
-4
-3
-6
-4
-6
-6
-6
-7
-6
-6
-5
-5
-6
-4
-7
-3
-5
-4
-5
-4
-5
-5
-6
-5
-5
-4
-3
-5
-3
-4
-1
-4
-5
-4
-4
-4
-3
-7
-6
-3
-6
-5
-4
-6
-4
-5
-6
-3
-6
-6
-7
-5
-6
-...

result:

wrong answer 7th lines differ - expected: '-7', found: '-5'