#include<bits/stdc++.h>
#define inf (long long)1e12
#define add(u, v) to[++tot] = v, nxt[tot] = hd[u], hd[u] = tot
#define LL long long
using namespace std;
const int N = 100000 + 5;
int n, m;
int to[N << 1], nxt[N << 1], hd[N], tot;
LL p[N], dep[N], fa[N][19], dp[N][2], f[N][19][2][2];
string type;
void dfs(int u) {
dp[u][1] = p[u];
for (int i = hd[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa[u][0]) continue;
dep[v] = dep[u] + 1; fa[v][0] = u;
dfs(v);
dp[u][1] += min(dp[v][0], dp[v][1]);
dp[u][0] += dp[v][1];
}
}
void init(void) {
dep[1] = 1; dfs(1);
for (int i = 1; i <= n; i++) {
f[i][0][0][0] = inf;
f[i][0][1][0] = dp[fa[i][0]][0] - dp[i][1];
f[i][0][1][1] = f[i][0][0][1] = dp[fa[i][0]][1] - min(dp[i][0], dp[i][1]);
}
for (int k = 1; k <= 18; k++) {
for (int i = 1; i <= n; i++) {
int F = fa[i][k - 1];
fa[i][k] = fa[F][k - 1];
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
f[i][k][x][y] = min(f[i][k - 1][x][0] + f[F][k - 1][0][y], f[i][k - 1][x][1] + f[F][k - 1][1][y]);
}
}
}
}
}
inline void Work(int u, int x, int v, int y) {
if(dep[u] < dep[v]) swap(u, v), swap(x, y);
LL a[2] = {inf, inf}, b[2] = {inf, inf}, res[2] = {inf, inf}, tmp[2]; int lca;
a[x] = dp[u][x], b[y] = dp[v][y];
for(int k = 18; k >= 0; k--) if (dep[fa[u][k]] >= dep[v]){
for(int i = 0; i < 2; i++) tmp[i] = a[i];
for(int i = 0; i < 2; i++) a[i] = min(tmp[0] + f[u][k][0][i], tmp[1] + f[u][k][1][i]);
u = fa[u][k];
}
if(u == v) {
lca = u;
res[y] = a[y];
}
else {
for(int k = 18; k >= 0; k--) if (fa[u][k] != fa[v][k]) {
for(int i = 0; i < 2; i++) tmp[i] = a[i];
for(int i = 0; i < 2; i++) a[i] = min(tmp[0] + f[u][k][0][i], tmp[1] + f[u][k][1][i]);
for(int i = 0; i < 2; i++) tmp[i] = b[i];
for(int i = 0; i < 2; i++) b[i] = min(tmp[0] + f[v][k][0][i], tmp[1] + f[v][k][1][i]);
u = fa[u][k], v = fa[v][k];
}
lca = fa[u][0];
res[0] = dp[lca][0] - dp[u][1] - dp[v][1] + a[1] + b[1];
res[1] = dp[lca][1] - min(dp[u][0], dp[u][1]) - min(dp[v][0], dp[v][1]) + min(a[0], a[1]) + min(b[0], b[1]);
}
for(int k = 18; k >= 0; --k) if (dep[fa[lca][k]]) {
for(int i = 0; i < 2; ++i) tmp[i] = res[i];
for(int i = 0; i < 2; ++i) res[i] = min(tmp[0] + f[lca][k][0][i], tmp[1] + f[lca][k][1][i]);
lca = fa[lca][k];
}
LL ans = min(res[0], res[1]);
printf("%lld\n", ans < inf ? ans : -1);
}
int main() {
cin >> n >> m >> type;
for (int i = 1; i <= n; i++) scanf("%lld", &p[i]);
for (int i = 1; i <= n - 1; i++) {
int u, v; scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
init();
while (m--) {
int u, x, v, y;
scanf("%d%d%d%d", &u, &x, &v, &y);
Work(u, x, v, y);
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 48.92 us | 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 48.55 us | 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 49.91 us | 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 46.2 us | 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 128.35 us | 164 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 125.94 us | 164 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 127.03 us | 156 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 1.682 ms | 1 MB + 804 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 1.687 ms | 1 MB + 804 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 1.647 ms | 1 MB + 716 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 1.646 ms | 1 MB + 716 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 147.769 ms | 86 MB + 156 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 147.612 ms | 86 MB + 156 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 153.202 ms | 85 MB + 984 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 153.264 ms | 85 MB + 988 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 153.335 ms | 85 MB + 988 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 211.66 ms | 86 MB + 156 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 132.586 ms | 78 MB + 536 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 132.747 ms | 78 MB + 536 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 139.648 ms | 81 MB + 980 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 139.623 ms | 81 MB + 980 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 145.727 ms | 81 MB + 784 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 196.045 ms | 81 MB + 976 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 196.039 ms | 81 MB + 980 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 196.121 ms | 81 MB + 992 KB | Accepted | Score: 4 | 显示更多 |