既然我们只知道最后数量为$k$的蚂蚁会在特殊边上被吃掉,不妨逆着推回去,然后到达每个叶节点的时候就会有一个被吃掉的蚂蚁的区间,然后二分一下就好啦
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,maxx=1e9;
int ant[N],deg[N],leaf[N];
int p[N],noww[*N],goal[*N];
int n,g,k,t,root,t1,t2,n1,n2,cnt;
long long ans;
void link(int f,int t)
{
noww[++cnt]=p[f];
goal[cnt]=t,p[f]=cnt;
}
void DFS(int nde,int fth,long long l,long long r)
{
if(l>maxx) return ; if(r>maxx) r=maxx;
for(int i=p[nde];i;i=noww[i])
if(goal[i]!=fth)
t=deg[goal[i]]-,DFS(goal[i],nde,l*t,r*t+t-);
if(leaf[nde])
ans+=(upper_bound(ant+,ant++g,r)-lower_bound(ant+,ant++g,l))*k;
}
int main ()
{
scanf("%d%d%d",&n,&g,&k);
for(int i=;i<=g;i++)
scanf("%d",&ant[i]);
sort(ant+,ant++g);
scanf("%d%d",&n1,&n2);
link(n1,n2),link(n2,n1);
deg[n1]++,deg[n2]++;
for(int i=;i<n;i++)
{
scanf("%d%d",&t1,&t2);
link(t1,t2),link(t2,t1);
deg[t1]++,deg[t2]++;
}
for(int i=;i<=n;i++) if(deg[i]==) deg[i]=,leaf[i]=true;
root=n1,DFS(n1,n2,k*(deg[n1]-),k*(deg[n1]-)+deg[n1]-);
root=n2,DFS(n2,n1,k*(deg[n2]-),k*(deg[n2]-)+deg[n2]-);
printf("%lld",ans);
return ;
}