BNUOJ-26482 Juice 树形DP

时间:2022-12-04 17:16:15

  题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26482

  题意:给一颗树,根节点为送电站,可以无穷送电,其它节点为house,电量达到pi时可以点亮,边为电线,传输有容量上限,求最多点亮多少个house。。

  简单树形DP,f[i][j]表示第 i 个节点电量为 j 时最多点亮的house个数。那么f[u][j]=Max{ f[u][j], f[u][j-k]+f[v][k] | v为u的儿子节点 }。。

 //STATUS:C++_AC_208MS_2032KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
//#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e9+,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,w;
}e[N];
int first[N],next[N];
int n,mt;
int p[N],c[N],f[N][],vis[N]; void adde(int a,int b,int c)
{
e[mt].u=a,e[mt].v=b;e[mt].w=c;
next[mt]=first[a],first[a]=mt++;
} void dfs(int u,int up)
{
int v,i,j,k;
vis[u]=;
f[u][p[u]]=;
for(i=first[u];i!=-;i=next[i]){
v=e[i].v;
dfs(v,e[i].w);
for(j=up;j>=;j--){
for(k=;k<=j && k<=e[i].w;k++){
f[u][j]=Max(f[u][j],f[u][j-k]+f[v][k]);
}
}
}
} int main(){
// freopen("in.txt","r",stdin);
int i,j,k,a,b,c;
while(~scanf("%d",&n))
{
mem(first,-);mt=;
for(i=;i<=n;i++){
scanf("%d%d%d",&a,&p[i],&c);
adde(a,i,c);
} mem(vis,);mem(f,);
int ans=;
for(i=first[];i!=-;i=next[i]){
int v=e[i].v;
dfs(v,e[i].w);
int hig=;
for(j=;j<=e[i].w;j++)hig=Max(hig,f[v][j]);
ans+=hig;
}
for(i=;i<=n;i++){
if(!vis[i] && !p[i])ans++;
} printf("%d\n",ans);
}
return ;
}