#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=2e5,Maxk=2e3;
const ll inf=1e18;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,K,val[Maxn+5];
vector<int> v[Maxn+5],w[Maxn+5];
namespace T1
{
int fa[Maxn+5],dep[Maxn+5],siz[Maxn+5],son[Maxn+5];
int top[Maxn+5]; ll dis[Maxn+5];
inline void dfs1(int x,int f)
{
fa[x]=f,dep[x]=dep[f]+1,siz[x]=1,dis[x]=dis[f]+val[x];
for(auto y:v[x]) if(y!=f)
{
dfs1(y,x),siz[x]+=siz[y];
son[x]=(siz[y]>siz[son[x]]?y:son[x]);
}
}
inline void dfs2(int x,int t)
{
top[x]=t;
if(son[x]) dfs2(son[x],t);
for(auto y:v[x]) if(y!=fa[x] && y!=son[x]) dfs2(y,y);
}
inline int LCA(int x,int y)
{
while(top[x]!=top[y]) (dep[top[x]]>dep[top[y]]?x=fa[top[x]]:y=fa[top[y]]);
return (dep[x]<dep[y]?x:y);
}
inline void Run()
{
dfs1(1,0),dfs2(1,1);
while(m--)
{
int x=read(),y=read(),l=LCA(x,y);
ll ans=dis[x]+dis[y]-2ll*dis[l]+val[l];
printf("%lld\n",ans);
}
}
}
namespace T3
{
int fa[Maxn+5],dep[Maxn+5];
int st[Maxn+5],top;
ll f[Maxn+5];
inline void dfs(int x,int f)
{
fa[x]=f,dep[x]=dep[f]+1;
for(auto y:v[x]) if(y!=f) dfs(y,x);
}
inline int LCA(int x,int y)
{
while(x!=y) (dep[x]>dep[y]?x=fa[x]:y=fa[y]);
return x;
}
inline ll Count(int x,int y)
{
int l=LCA(x,y); top=0;
int a=x;
while(a!=l) st[++top]=a,a=fa[a];
st[++top]=l;
int it=top; a=y;
while(a!=l) st[++top]=a,a=fa[a];
reverse(st+it+1,st+top+1);
For(i,1,top) f[i]=inf;
f[1]=val[st[1]];
For(i,2,top)
{
f[i]=f[i-1]+val[st[i]];
For(j,2,min(i-1,K)) f[i]=min(f[i],f[i-j]+val[st[i]]);
}
return f[top];
}
inline void Run()
{
dfs(1,0);
while(m--)
{
int x=read(),y=read();
printf("%lld\n",Count(x,y));
}
}
}
inline int cmp(int x,int y) {return val[x]<val[y];}
namespace T4
{
int fa[Maxn+5],dep[Maxn+5],vis[Maxn+5];
int st[Maxn+5],son[Maxn+5],top;
ll f[Maxn+5][2];
inline void dfs(int x,int f)
{
fa[x]=f,dep[x]=dep[f]+1;
for(auto y:v[x]) if(y!=f) dfs(y,x);
}
inline int LCA(int x,int y)
{
while(x!=y) (dep[x]>dep[y]?x=fa[x]:y=fa[y]);
return x;
}
inline ll Count(int x,int y)
{
int l=LCA(x,y); top=0;
int a=x;
while(a!=l) st[++top]=a,a=fa[a];
st[++top]=l;
int it=top; a=y;
while(a!=l) st[++top]=a,a=fa[a];
reverse(st+it+1,st+top+1);
For(i,1,top) f[i][0]=f[i][1]=inf,vis[st[i]]=1;
For(i,1,top)
{
son[i]=0;
for(auto y:v[st[i]]) if(!vis[y]) {son[i]=y; break;}
}
// For(i,1,top) printf("%d: %d %d\n",i,st[i],son[i]);
f[1][0]=val[st[1]];
if(son[1]) f[1][1]=val[st[1]]+val[son[1]];
// printf("%lld %lld\n",f[1][0],f[1][1]);
For(i,2,top)
{
f[i][0]=f[i-1][0]+val[st[i]];
if(i>2) f[i][0]=min(f[i][0],f[i-2][0]+val[st[i]]);
if(i>3) f[i][0]=min(f[i][0],f[i-3][0]+val[st[i]]);
f[i][0]=min(f[i][0],f[i-1][1]+val[st[i]]);
if(i>2) f[i][0]=min(f[i][0],f[i-2][1]+val[st[i]]);
if(son[i])
{
f[i][1]=f[i][0]+val[son[i]];
f[i][1]=min(f[i][1],f[i-1][0]+val[son[i]]);
f[i][1]=min(f[i][1],f[i-1][1]+val[son[i]]);
if(i>2) f[i][1]=min(f[i][1],f[i-2][0]+val[son[i]]);
}
// printf("%lld %lld\n",f[i][0],f[i][1]);
}
For(i,1,top) vis[st[i]]=0;
return f[top][0];
}
inline void Run()
{
For(i,1,n) sort(v[i].begin(),v[i].end(),cmp);
dfs(1,0);
while(m--)
{
int x=read(),y=read();
printf("%lld\n",Count(x,y));
}
}
}
int main()
{
freopen("transmit.in","r",stdin);
freopen("transmit.out","w",stdout);
n=read(),m=read(),K=read();
For(i,1,n) val[i]=read();
For(i,1,n-1)
{
int a=read(),b=read();
v[a].push_back(b),v[b].push_back(a);
}
if(K==1) {T1::Run(); return 0;}
if(K==3) {T4::Run(); return 0;}
T3::Run();
return 0;
}剪贴板
剪贴板 qwpf81fq
剪贴板信息
剪贴板信息
- 收录
- 更新