正解:二分+$dp$
解题报告:
一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$.
考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶子节点就成$QwQ$
$over$
嗷注意一个细节是有可能乘爆,所以每次和$m_max$取个$min$就成$QwQ$
#include<bits/stdc++.h> using namespace std; #define il inline #define gc getchar() #define int long long #define t(i) edge[i].to #define ri register int #define rc register char #define rb register bool #define rp(i,x,y) for(ri i=x;i<=y;++i) #define my(i,x,y) for(ri i=x;i>=y;--i) #define e(i,x) for(ri i=head[x];i;i=edge[i].nxt) const int N=1000000+10; int n,head[N],ed_cnt,g,K,m[N],frx,fry,in[N],l[N],r[N],inf,as; struct ed{int to,nxt;}edge[N<<1]; il int read() { rc ch=gc;ri x=0;rb y=1; while(ch!='-' && (ch>'9' || ch<'0'))ch=gc; if(ch=='-')ch=gc,y=0; while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc; return y?x:-x; } il void ad(ri x,ri y){edge[++ed_cnt]=(ed){x,head[y]};head[y]=ed_cnt;++in[x];} void dfs(ri nw,ri fa) { e(i,nw) if(t(i)^fa) l[t(i)]=min(1ll*l[nw]*(in[nw]-1),inf+1),r[t(i)]=min(1ll*(r[nw]+1)*(in[nw]-1)-1,inf),dfs(t(i),nw); } il int fd1(ri x){return lower_bound(m+1,m+1+g,x)-m-1;} il int fd2(ri x){ri tmp=upper_bound(m+1,m+1+g,x)-m;return m[tmp]==x?tmp:tmp-1;} signed main() { freopen("3872.in","r",stdin);freopen("3872.out","w",stdout); n=read();g=read();K=read();rp(i,1,g)m[i]=read();sort(m+1,m+1+g);inf=m[g]; rp(i,1,n-1){ri x=read(),y=read();ad(x,y);ad(y,x);if(i==1)frx=x,fry=y;} l[frx]=r[frx]=l[fry]=r[fry]=K;dfs(frx,fry);dfs(fry,frx); rp(i,1,n)if(in[i]==1)as+=fd2(r[i])-fd1(l[i]);;printf("%lld ",1ll*as*K); return 0; }