\(\text{Description}\)
\(\text{Solution}\)
我的我要爆了!
有一个结论:整棵树选 \(x\) 个点的连通子图黑点数目最小值/最大值分别为 \(\min,\max\),那么在 \([\min,\max]\) 中的 \(y\) 值都是可以选取的。
考虑我们从最小值的状态往最大值的状态慢慢挪动,每次删一个点加一个点,只可能有一个黑点的变化,那如果想要挪过去,显然每一种黑点数都是存在的。
所以就大力用树形背包算整棵树选 \(x\) 个点的连通子图黑点数目最小值/最大值就行了。
复杂度 \(\mathcal O(n^2)\)。
\(\text{Ad}\)
树形背包复杂度证明👉:传送门。
\(\text{Code}\)
#include <cstdio>
#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
template <class T> inline void write(const T x) {
if(x<0) return (void) (putchar('-'),write(-x));
if(x>9) write(x/10);
putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
#include <cstring>
const int maxn=5005;
int n,q,head[maxn],nxt[maxn<<1],to[maxn<<1],cnt,siz[maxn],f[maxn][maxn],g[maxn][maxn],val[maxn];
void addEdge(int u,int v) {
nxt[++cnt]=head[u],to[cnt]=v,head[u]=cnt;
nxt[++cnt]=head[v],to[cnt]=u,head[v]=cnt;
}
void DP(int u,int fa) {
siz[u]=1; f[u][1]=g[u][1]=val[u];
erep(i,u) {
if(v==fa) continue;
DP(v,u);
fep(j,siz[u],1)
fep(k,siz[v],1)
f[u][j+k]=Min(f[u][j+k],f[u][j]+f[v][k]),
g[u][j+k]=Max(g[u][j+k],g[u][j]+g[v][k]);
siz[u]+=siz[v];
}
rep(i,1,n) f[0][i]=Min(f[0][i],f[u][i]),g[0][i]=Max(g[0][i],g[u][i]);
}
int main() {
int u,v; memset(f,0x3f,sizeof f);
read(9),n=read(9),q=read(9);
rep(i,1,n-1) {
u=read(9),v=read(9);
addEdge(u,v);
}
rep(i,1,n) val[i]=read(9);
DP(1,0);
while(q--) {
u=read(9),v=read(9);
if(f[0][u]<=v&&g[0][u]>=v) puts("YES");
else puts("NO");
}
return 0;
}
浙公网安备 33010602011771号