#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <limits>
#include <functional>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct Edge {
int v;
Edge *next;
};
ll ad(ll x, ll y) {
if (~x & ~y)
return x + y;
return -1;
}
bool cmp(ll x, ll y) {
return x != -1 && (x < y || y == -1);
}
struct Mat {
ll gr[2][2];
Mat() { memset(gr, -1, sizeof(gr)); }
Mat(const Mat& rhs) { memcpy(gr, rhs.gr, sizeof(gr)); }
ll* operator[](int k) { return gr[k]; }
const ll* operator[](int k) const { return gr[k]; }
void dbg() const {
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 2; ++j) {
LOG("%lld ", gr[i][j]);
}
LOG("\n");
}
}
Mat operator*(const Mat& rhs) const {
Mat m = Mat();
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k)
m[i][j] = min(m[i][j], ad(gr[i][k], rhs.gr[k][j]), cmp);
}
// dbg();
// rhs.dbg();
// m.dbg();
return m;
}
};
const int N = 100010;
int n, m;
char s[5];
bool vis[N];
int p[N], f[N], qa[N], qx[N], qb[N], qy[N], lca[N];
Mat dt[N];
ll ans[N];
ll dp[N][2], tmp[N][2];
Edge* g[N];
vector<pair<int, int> > ql[N];
vector<int> qr[N];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int find2(int x) {
if (f[x] == x)
return x;
int prt = f[x];
f[x] = find2(prt);
dt[x] = dt[prt] * dt[x];
return f[x];
}
void adde(int u, int v);
void tarjan(int u);
void dfs(int u);
int main() {
scanf("%d%d%s", &n, &m, s);
for (int i = 1; i <= n; ++i)
scanf("%d", &p[i]);
for (int rep = 1; rep < n; ++rep) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v);
adde(v, u);
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d%d", &qa[i], &qx[i], &qb[i], &qy[i]);
ql[qa[i]].push_back(make_pair(qb[i], i));
ql[qb[i]].push_back(make_pair(qa[i], i));
}
tarjan(1);
for (int i = 1; i <= m; ++i)
qr[lca[i]].push_back(i);
memset(vis, 0, sizeof(vis));
memset(f, 0, sizeof(f));
{
Mat model = Mat();
model[0][0] = model[1][1] = 0;
fill(dt + 1, dt + n + 1, model);
}
dfs(1);
for (int i = 1; i <= m; ++i) {
int lc = lca[i];
find2(lc);
ans[i] = -1;
// LOG("%d: %lld %lld\n", i, tmp[i][0], tmp[i][1]);;
for (int k = 0; k < 2; ++k)
for (int j = 0; j < 2; ++j)
ans[i] = min(ans[i], ad(dt[lc][k][j], tmp[i][j]), cmp);
printf("%lld\n", ans[i]);
}
return 0;
}
void dfs(int u) {
vis[u] = true;
f[u] = u;
dp[u][1] = p[u];
for (Edge* p = g[u]; p; p = p->next)
if (!vis[p->v]) {
dfs(p->v);
dp[u][0] += dp[p->v][1];
dp[u][1] += min(dp[p->v][0], dp[p->v][1]);
}
for (int i = 0; i < qr[u].size(); ++i) {
int id = qr[u][i];
int &a = qa[id], &x = qx[id], &b = qb[id], &y = qy[id];
if (u == b) {
swap(a, b);
swap(x, y);
}
if (u != a) {
find2(a);
find2(b);
for (int k = 0; k < 2; ++k) {
tmp[id][k] = -1;
ll sub = dp[u][k];
if (k == 0)
sub -= dp[f[a]][1] + dp[f[b]][1];
else
sub -= min(dp[f[a]][0], dp[f[a]][1]) + min(dp[f[b]][0], dp[f[b]][1]);
if (k == 0)
tmp[id][0] = ad(dp[a][x] + dp[b][y], ad(sub, ad(dt[a][1][x], dt[b][1][y])));
else
tmp[id][1] = ad(dp[a][x] + dp[b][y] + sub, ad(min(dt[a][1][x], dt[a][0][x], cmp), min(dt[b][1][y], dt[b][0][y], cmp)));
}
}
}
for (Edge* p = g[u]; p; p = p->next)
if (!vis[p->v]) {
f[p->v] = u;
dt[p->v][0][0] = -1;
dt[p->v][0][1] = dp[u][0] - dp[p->v][1];
dt[p->v][1][0] = dt[p->v][1][1] = dp[u][1] - min(dp[p->v][0], dp[p->v][1]);
}
for (int i = 0; i < qr[u].size(); ++i) {
int id = qr[u][i];
int a = qa[id], x = qx[id], b = qb[id], y = qy[id];
if (u == a) {
find2(b);
// LOG("%d(from %d)%d %d\n", f[b], b, f[f[b]], u);
tmp[id][x] = ad(dt[b][x][y], dp[b][y]);
// LOG("%lld\n", dt[b][x][y]);
tmp[id][!x] = -1;
}
}
vis[u] = false;
}
void tarjan(int u) {
static bool vis[N];
vis[u] = true;
f[u] = u;
for (Edge* p = g[u]; p; p = p->next)
if (!vis[p->v]) {
tarjan(p->v);
f[p->v] = u;
}
for (int i = 0; i < ql[u].size(); ++i)
if (f[ql[u][i].first])
lca[ql[u][i].second] = find(ql[u][i].first);
}
void adde(int u, int v) {
static Edge pool[N * 2];
static Edge* p = pool;
p->v = v;
p->next = g[u];
g[u] = p;
++p;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.211 ms | 8 MB + 180 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 1.221 ms | 8 MB + 180 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 1.22 ms | 8 MB + 180 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 1.211 ms | 8 MB + 180 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 1.199 ms | 8 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 1.215 ms | 8 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 1.187 ms | 8 MB + 200 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 2.801 ms | 8 MB + 852 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 2.775 ms | 8 MB + 852 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 2.752 ms | 8 MB + 652 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 2.753 ms | 8 MB + 652 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 59.553 ms | 39 MB + 708 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 59.641 ms | 39 MB + 708 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 76.92 ms | 41 MB + 32 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 76.972 ms | 41 MB + 40 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 77.174 ms | 41 MB + 40 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 111.331 ms | 40 MB + 1000 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 72.035 ms | 22 MB + 932 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 71.939 ms | 22 MB + 936 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 58.852 ms | 30 MB + 484 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 58.816 ms | 30 MB + 476 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 73.845 ms | 31 MB + 256 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 101.007 ms | 31 MB + 116 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 100.968 ms | 31 MB + 136 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 101.19 ms | 31 MB + 156 KB | Accepted | Score: 4 | 显示更多 |