Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //debug
  4. #ifdef grief
  5. #define debug(...) do{\
  6. fprintf(stderr , "%s - %d : (%s) = " , __PRETTY_FUNCTION__ , __LINE__ , #__VA_ARGS__ );\
  7. _DO(__VA_ARGS__);\
  8. }while(0)
  9. template<typename I> void _DO(I&&x){ cerr<<x<<endl; }
  10. template<typename I,typename...T> void _DO(I&&x,T&&...tail){ cerr<<x<<" , "; _DO(tail...); }
  11. template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p)
  12. { return _s<<'('<<_p.first<<','<<_p.second<<')'; }
  13. #define IOS
  14. #else
  15. #define debug(...)
  16. #define IOS do{ios_base::sync_with_stdio(0); cin.tie(0);}while(0);
  17. #endif
  18. //type
  19. typedef long long ll;
  20. typedef pair<int,int> pii;
  21. typedef pair<ll,ll> pll;
  22. typedef pair<int,double> pp;
  23. typedef priority_queue<pll,vector<pll>,greater<pll> > heap;
  24. //macro
  25. #define SZ(x) ((ll)(x).size())
  26. #define ALL(x) (x).begin(),(x).end()
  27. #define F first
  28. #define S second
  29. #define pb push_back
  30. #define eb emplace_back
  31. #define stp setprecision(30)<<fixed
  32. const ll INF=0x3f3f3f3f3f3f3f3f;
  33. const int NF=0x3f3f3f3f;
  34. const ll MAX=1e3+5,Mlg=__lg(MAX)+2;
  35. const ll MOD=1e9+7;
  36. const double eps=1e-7;
  37. // ready~ go!
  38. // let's coding and have fun!
  39. // I can't solve this problem!
  40. int n,k,C[MAX],L[MAX],dp[MAX][100005];
  41. vector<int> son[MAX];
  42. void dfs(int u,int sz){
  43. for(int i:son[u]){
  44. for(int j=0;j<=sz-C[i];j++) dp[i][j]=dp[u][j];
  45. dfs(i,sz-C[i]);
  46. for(int j=C[i];j<=sz;j++) dp[u][j]=max(dp[u][j],dp[i][j-C[i]]+L[i]);
  47. }
  48. return;
  49. }
  50. int main(){
  51. IOS
  52. int T; cin>>T;
  53. while(T--){
  54. cin>>n>>k;
  55. for(int i=0;i<n;i++) son[i].clear();
  56. for(int i=1;i<=n;i++){
  57. int p;
  58. cin>>p>>C[i]>>L[i];
  59. son[p].eb(i);
  60. }
  61. memset(dp[0],0,k*4+4);
  62. dfs(0,k);
  63. cout<<dp[0][k]<< '\n';
  64. }
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement