QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504540 | #9110. Zayin and Tree | IllusionaryDominance# | AC ✓ | 35ms | 20020kb | C++20 | 1.5kb | 2024-08-04 13:45:57 | 2024-08-04 13:45:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
char rr[1 << 22], *csy1, *csy2;
#define GC (csy1 == csy2 && (csy2 = (csy1 = rr) + fread(rr, 1, 1 << 22, stdin), csy1 == csy2) ? EOF : *csy1 ++)
template <class T>
inline void RI(T &t) {
char c = GC;
for (t = 0; c < 48 || c > 57; c = GC);
for ( ; c > 47 && c < 58; c = GC) t = t * 10 + (c ^ 48);
}
const int MAX_N = 1000000 + 5;
int N, a[MAX_N], f[MAX_N][2], dep[MAX_N], ans;
struct Edge{
int y, prev;
}e[MAX_N << 1];
int elast[MAX_N], ecnt;
void Build(int x, int y) {
ecnt ++;
e[ecnt].y = y;
e[ecnt].prev = elast[x];
elast[x] = ecnt;
}
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
f[u][0] = a[u] + dep[u];
f[u][1] = dep[u] - a[u];
for (int i = elast[u]; i; i = e[i].prev) {
int v = e[i].y;
if (v != fa) {
dfs(v, u);
ans = min(ans, min(f[u][0] + f[v][1] - dep[u] * 2 + 1, f[u][1] + f[v][0] - dep[u] * 2 + 1));
f[u][0] = min(f[u][0], f[v][0]);
f[u][1] = min(f[u][1], f[v][1]);
}
}
}
void solve() {
ecnt = 0;
memset(elast, 0, sizeof(int) * (N + 1));
RI(N);
for (int i = 1; i <= N; i ++) RI(a[i]);
for (int i = 1; i < N; i ++) {
int x, y;
RI(x); RI(y);
Build(x, y); Build(y, x);
}
ans = 1;
dfs(1, 0);
printf("%d\n", ans);
}
int main() {
int T;
RI(T);
while (T --) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 35ms
memory: 20020kb
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