小A从仓库里找出了一棵n个点的有根树,1号节点为这棵树的根,树上每个节点的权值为wi, 大小为ai。
现在他心中产生了Q个疑问,每个疑问形如在x的子树里,选出一些大小和不超过s的节点(不可以重复选一个节点),最大权值和可以为多少。
一行一个整数n。
n−1行两个整数ui, vi表示一条边。
N每行两个整数wi, ai表示这个点的权值和大小。
一行一个整数Q。
每行两个整数x, s,表示一个询问。
Q行每行一个整数表示答案
5
1 3
2 4
5 3
4 3
2 3
3 2
1 4
5 4
3 1
7
1 5
2 1
2 2
2 3
4 4
3 3
3 5
8
0
3
3
5
6
8
10%的数据满足n≤10,s,ai≤10,wi≤10
100%的数据满足n,s,ai≤5000,wi≤10^6,q<=10^5
1s, 512MB
分析:
10分----对每个询问暴力背包DP即可
30分----设f[x][s]表示以x为根节点的子树中取大小和不超过x的节点,最大权值和是多少
F[x][s]=f[c][s’]+f[x][s-s’] (c为子树的节点,合并时的状态转移方程)
时间复杂度为O(ns^2)
100分----之前算法的瓶颈在于合并时的复杂度较高,可以使用启发式合并,每次合并的时候暴力DFS物品较少的子树,直接把一个物品最多的孩子DP数组拷贝到父亲节点的DP 数组上,对于其他孩子暴力DFS,一个一个加进来,复杂度为O(n s log n)
Code
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define reg(i,a) for(int i=u[a];i;i=e[i].pre)
#define v e[i].to
#define LL long long
using namespace std;
const int maxn=5010;
struct edge
{
int to,pre;
}e[maxn<<1];
int u[maxn],cnt=0;
int n,son[maxn],sz[maxn],w[maxn],l[maxn];
LL f[maxn][maxn];
inline void ins(int a,int b)
{
e[++cnt]=(edge){b,u[a]};
u[a]=cnt;
}
inline int read()
{
int f=1,x=0;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;
}
inline LL maxLL(LL a,LL b){return a>b?a:b;}
void dfs(int x,int f)
{
sz[x]=1,son[x]=0;
reg(i,x) if(v!=f)dfs(v,x),sz[x]+=sz[v],son[x]=(sz[v]>sz[son[x]]?v:son[x]);//son[x]表示物品最多的子节点编号
}
void add(int x,int fa,LL f[])
{
dep(i,maxn-10,l[x]) f[i]=maxLL(f[i],f[i-l[x]]+w[x]);
reg(i,x) if(v!=fa) add(v,x,f);
}
void dp(int x,int fa)
{
reg(i,x) if(v!=fa)dp(v,x);
memcpy(f[x],f[son[x]],sizeof(f[x]));//直接拷贝最大的儿子DP数组给父亲节点
reg(i,x) if(v!=fa&&v!=son[x]) add(v,x,f[x]);//dfs加上其他子节点的物品
dep(j,maxn-10,l[x]) f[x][j]=maxLL(f[x][j],f[x][j-l[x]]+w[x]);
}
int main()
{
n=read();
rep(i,1,n-1){int a=read(),b=read();ins(a,b);ins(b,a);}
rep(i,1,n){w[i]=read(),l[i]=read();}
dfs(1,0);
dp(1,0);//离线做法
int q=read();
rep(i,1,q){int x=read(),s=read();cout<<f[x][s]<<endl;}
return 0;
}
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net ...
仅仅出现在tf2.1中,在tf2.4中已经不存在该问题。 这个问题查询了很多教程都没有很好的解决,其中有一个比较有启发的解决方案是这样的。 https://blog.csdn.net/dldldl0/article/details/79089074 ValueError: The last dimension of the inputs to Dense should be defined. Fo...
[size=large][color=darkred][b]题目描述[/b][/color][/size] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last ...
题目地址: https://leetcode.com/problems/length-of-last-word/description/ 我的思路是去掉尾部所有空字符,然后计算count 直到遇到空字符为止...
题目描述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is de...
题目位置 https://www.lintcode.com/problem/last-position-of-target/description 题目描述 Find the last position of a target number in a sorted array. Return -1 if target does not exist. Example Example 1: Input...
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined...
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A ...
4.11.1逗号代码 4.11.2掷硬币的连胜 4.11.3字符图网格...