#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
int read()
{
int out=0;
char c;
while (!isdigit(c=getchar()));
for (;isdigit(c);c=getchar()) out=out*10+c-'0';
return out;
}
const int N=100010;
const long long INF=1e10;
void dfs1(int u);
void dfs2(int u);
void add(int u,int v);
int head[N],nxt[N<<1],to[N<<1],cnt;
long long n,m,p[N],f[N][2],g[N][2],bz[N][20][2][2],fa[N][20],dep[N],ans[2][2],anss[2][2][2],ansss;
char type[20];
int main()
{
int i,u,v,a,b,x,y,cur=0;
n=read();m=read();
scanf("%s",type);
for (i=1;i<=n;++i) p[i]=read();
for (i=1;i<n;++i)
{
u=read();
v=read();
add(u,v);
add(v,u);
}
fa[1][0]=1;
dfs1(1);
dfs2(1);
for (i=1;i<=16;++i)
{
for (u=1;u<=n;++u)
{
fa[u][i]=fa[fa[u][i-1]][i-1];
bz[u][i][0][1]=min(bz[u][i-1][0][0]+bz[fa[u][i-1]][i-1][0][1],bz[u][i-1][0][1]+bz[fa[u][i-1]][i-1][1][1]);
bz[u][i][1][0]=min(bz[u][i-1][1][0]+bz[fa[u][i-1]][i-1][0][0],bz[u][i-1][1][1]+bz[fa[u][i-1]][i-1][1][0]);
bz[u][i][1][1]=min(bz[u][i-1][1][0]+bz[fa[u][i-1]][i-1][0][1],bz[u][i-1][1][1]+bz[fa[u][i-1]][i-1][1][1]);
bz[u][i][0][0]=min(bz[u][i-1][0][0]+bz[fa[u][i-1]][i-1][0][0],bz[u][i-1][0][1]+bz[fa[u][i-1]][i-1][1][0]);
}
}
while (m--)
{
a=read();
x=read();
b=read();
y=read();
u=a;
v=b;
if (dep[u]<dep[v])
{
swap(u,v);
swap(a,b);
swap(x,y);
}
ans[cur][x]=f[u][x];
ans[cur][x^1]=INF;
for (i=16;i>=0;--i)
{
if ((dep[u]-dep[v])&(1<<i))
{
cur^=1;
ans[cur][0]=min(bz[u][i][0][0]+ans[cur^1][0],bz[u][i][1][0]+ans[cur^1][1]);
ans[cur][1]=min(bz[u][i][0][1]+ans[cur^1][0],bz[u][i][1][1]+ans[cur^1][1]);
u=fa[u][i];
}
}
if (u==v)
{
if (ans[cur][y]+g[u][y]>=INF)
{
puts("-1");
continue;
}
printf("%lld\n",ans[cur][y]+g[u][y]);
continue;
}
anss[cur][0][y]=ans[cur][0]+f[v][y];
anss[cur][1][y]=ans[cur][1]+f[v][y];
anss[cur][0][y^1]=anss[cur][1][y^1]=INF;
for (i=16;i>=0;--i)
{
if (fa[u][i]!=fa[v][i])
{
cur^=1;
anss[cur][0][0]=min(min(bz[u][i][0][0]+bz[v][i][0][0]+anss[cur^1][0][0],
bz[u][i][1][0]+bz[v][i][0][0]+anss[cur^1][1][0]),
min(bz[u][i][0][0]+bz[v][i][1][0]+anss[cur^1][0][1],
bz[u][i][1][0]+bz[v][i][1][0]+anss[cur^1][1][1]));
anss[cur][0][1]=min(min(bz[u][i][0][0]+bz[v][i][0][1]+anss[cur^1][0][0],
bz[u][i][1][0]+bz[v][i][0][1]+anss[cur^1][1][0]),
min(bz[u][i][0][0]+bz[v][i][1][1]+anss[cur^1][0][1],
bz[u][i][1][0]+bz[v][i][1][1]+anss[cur^1][1][1]));
anss[cur][1][0]=min(min(bz[u][i][0][1]+bz[v][i][0][0]+anss[cur^1][0][0],
bz[u][i][1][1]+bz[v][i][0][0]+anss[cur^1][1][0]),
min(bz[u][i][0][1]+bz[v][i][1][0]+anss[cur^1][0][1],
bz[u][i][1][1]+bz[v][i][1][0]+anss[cur^1][1][1]));
anss[cur][1][1]=min(min(bz[u][i][0][1]+bz[v][i][0][1]+anss[cur^1][0][0],
bz[u][i][1][1]+bz[v][i][0][1]+anss[cur^1][1][0]),
min(bz[u][i][0][1]+bz[v][i][1][1]+anss[cur^1][0][1],
bz[u][i][1][1]+bz[v][i][1][1]+anss[cur^1][1][1]));
u=fa[u][i];
v=fa[v][i];
}
}
ansss=min(min(min(anss[cur][0][0],anss[cur][0][1]),min(anss[cur][1][0],anss[cur][1][1]))+g[fa[u][0]][1]+f[fa[u][0]][1]-min(f[u][0],f[u][1])-min(f[v][0],f[v][1]),anss[cur][1][1]+g[fa[u][0]][0]+f[fa[u][0]][0]-f[u][1]-f[v][1]);
if (ansss>=INF)
{
puts("-1");
continue;
}
printf("%lld\n",ansss);
}
return 0;
}
void dfs1(int u)
{
int i,v;
f[u][1]=p[u];
for (i=head[u];i;i=nxt[i])
{
v=to[i];
if (v!=fa[u][0])
{
fa[v][0]=u;
dep[v]=dep[u]+1;
dfs1(v);
f[u][0]+=f[v][1];
f[u][1]+=min(f[v][0],f[v][1]);
}
}
}
void dfs2(int u)
{
int i,v;
for (i=head[u];i;i=nxt[i])
{
v=to[i];
if (v!=fa[u][0])
{
g[v][0]=g[u][1]+f[u][1]-min(f[v][0],f[v][1]);
g[v][1]=min(g[u][0]+f[u][0]-f[v][1],g[u][1]+f[u][1]-min(f[v][0],f[v][1]));
bz[v][0][0][1]=bz[v][0][1][1]=f[u][1]-min(f[v][0],f[v][1]);
bz[v][0][1][0]=f[u][0]-f[v][1];
bz[v][0][0][0]=INF;
dfs2(v);
}
}
}
void add(int u,int v)
{
nxt[++cnt]=head[u];
head[u]=cnt;
to[cnt]=v;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 44.38 us | 84 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 50.69 us | 84 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 52.26 us | 84 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 44.11 us | 84 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 91.45 us | 168 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 106.23 us | 168 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 106.7 us | 160 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 1.204 ms | 1 MB + 916 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 1.211 ms | 1 MB + 916 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 1.265 ms | 1 MB + 828 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 1.252 ms | 1 MB + 828 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 122.722 ms | 91 MB + 508 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 122.622 ms | 91 MB + 508 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 109.821 ms | 91 MB + 312 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 109.764 ms | 91 MB + 316 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 109.757 ms | 91 MB + 316 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 163.285 ms | 91 MB + 508 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 112.794 ms | 83 MB + 888 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 112.569 ms | 83 MB + 888 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 114.476 ms | 87 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 114.562 ms | 87 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 105.386 ms | 87 MB + 112 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 155.381 ms | 87 MB + 304 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 155.485 ms | 87 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 155.253 ms | 87 MB + 320 KB | Accepted | Score: 4 | 显示更多 |