A small tree in

A small became a gardener! He has a tree of n nodes (if you do not know what trees are, see Hint part). He accidentally knocked over the inkwell that some tree nodes are blacked out. A small tree found blackened tree is very beautiful, so would remove a link subgraph x points from the tree, so that these points exactly y black spots, he wants to know whether he wishes to achieve. But he is too small, will not be considered, please help him.

Can be found in the size of the same number of black spots where sub-graphs is a contiguous interval, then practice with the farmer money

#include<bits/stdc++.h>
using namespace std;
int t ,n,q,a[5001],sz[5001],f[5001][5001],g[5001][5001],ff[5001],gg[5001];
vector<int> e[5001];
void dfs(int u,int fa){
    int i,j,k;
    sz[u]=1;f[u][a[u]]=g[u][a[u]]=1;
    for(i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(v==fa)continue;
        dfs(v,u);
        memcpy(ff,f[u],sizeof(f[u])),memcpy(gg,g[u],sizeof(g[u]));
        for(j=sz[u];j>=a[u];j--){
            for(k=sz[v];k>=a[v];k--){
                ff [j + k] = max (ff [j + k], f [u] [j] + f [v] [k]);
                gg[j+k]=min(gg[j+k],g[u][j]+g[v][k]);    
            }
        }
        sz[u]+=sz[v];
        for(j=a[u];j<=sz[u];j++)f[u][j]=ff[j],g[u][j]=gg[j];
    }
    for(i=0;i<=sz[u];i++)f[0][i]=max(f[0][i],f[u][i]),g[0][i]=min(g[0][i],g[u][i]);
    return ;
}
int main () {
    memset(g,0x3f,sizeof(g));
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&q);
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        int i;
        for(i=1;i<n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y),e[y].push_back(x);
        }
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        dfs(1,0);
        while(q--){
            int x,y;
            scanf("%d%d",&x,&y);
            if(g[0][y]<=x&&f[0][y]>=x)puts("YES");
            else puts("NO");
        } 
    }
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/zbsakioi/p/11032753.html