QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504498#9110. Zayin and TreePlentyOfPenalty#AC ✓176ms16944kbC++201.3kb2024-08-04 13:32:202024-08-04 13:32:21

Judging History

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

  • [2024-08-04 13:32:21]
  • 评测
  • 测评结果:AC
  • 用时:176ms
  • 内存:16944kb
  • [2024-08-04 13:32:20]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define rep(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
#define per(i, l, r) for (int i = (l), i##end = (r); i >= i##end; --i)
#ifdef memset0
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define log(...) (void(0))
#define endl '\n'
#endif
using namespace std;
using ll = long long;
using lf = long double;
using ull = unsigned long long;
const int N = 1e6;
int T, n, a[N + 10];
int u, v;
int mn1[N + 10], mn2[N + 10], ans;
vector<int> e[N + 10];
void dfs(int x, int fa, int d) {
  mn1[x] = d - a[x];
  mn2[x] = d + a[x];
  for (int i : e[x])
    if (i != fa) {
      dfs(i, x, d + 1);
      ans = min(ans, mn1[x] + mn2[i] - (d << 1) + 1);
      ans = min(ans, mn2[x] + mn1[i] - (d << 1) + 1);
      mn1[x] = min(mn1[x], mn1[i]);
      mn2[x] = min(mn2[x], mn2[i]);
    }
}
int main() {
#ifdef memset0
  freopen("J.in", "r", stdin);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) vector<int>().swap(e[i]);
    for (int i = 1; i < n; ++i) {
      cin >> u >> v;
      e[u].push_back(v), e[v].push_back(u);
    }
    ans = 1;
    dfs(1, 0, 1);
    cout << ans << "\n";
  }
}

详细

Test #1:

score: 100
Accepted
time: 176ms
memory: 16944kb

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

result:

ok 3009 lines