zoukankan      html  css  js  c++  java
  • The more, The Better

    The more, The Better
    依赖背包+树形dp
    f[x][j+1]=max(f[x][j+1],f[x][j+1-k]+f[i->n][k]);
    我的一个疑问就是这k个节点会不会选重复,答案是不会的,因为从一开始就不会重复,根据dp原理回溯的时候过程是一样的,也是不会重复的

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<set>
     8 #include<map>
     9 #include<stack>
    10 #include<cstring>
    11 #define inf 2147483647
    12 #define ls rt<<1
    13 #define rs rt<<1|1
    14 #define lson ls,nl,mid,l,r
    15 #define rson rs,mid+1,nr,l,r
    16 #define N 100010
    17 #define For(i,a,b) for(int i=a;i<=b;i++)
    18 #define p(a) putchar(a)
    19 #define g() getchar()
    20 
    21 using namespace std;
    22 int f[300][300];
    23 int a[300];
    24 int n,num;
    25 int x,y,m;
    26 
    27 struct node{
    28     int n;
    29     node *next;
    30 }*e[100000];
    31 
    32 void in(int &x){
    33     int y=1;
    34     char c=g();x=0;
    35     while(c<'0'||c>'9'){
    36         if(c=='-')y=-1;
    37         c=g();
    38     }
    39     while(c<='9'&&c>='0'){
    40         x=(x<<1)+(x<<3)+c-'0';c=g();
    41     }
    42     x*=y;
    43 }
    44 void o(int x){
    45     if(x<0){
    46         p('-');
    47         x=-x;
    48     }
    49     if(x>9)o(x/10);
    50     p(x%10+'0');
    51 }
    52 
    53 void dfs(int x,int m){
    54     
    55     f[x][1]=a[x];
    56     for(node *i=e[x];i;i=i->next){
    57         if(m>1)
    58             dfs(i->n,m-1);
    59         for(int j=m-1;j;j--)
    60             For(k,1,j)
    61                 f[x][j+1]=max(f[x][j+1],f[x][j+1-k]+f[i->n][k]);
    62     }
    63 }
    64 
    65 void push(int x,int y){
    66     node *p;
    67     p=new node();
    68     p->n=y;
    69     if(e[x]==0)
    70         e[x]=p;
    71     else{
    72         p->next=e[x]->next;
    73         e[x]->next=p;
    74     }
    75 }
    76 
    77 int main(){
    78     while(cin>>n>>m&&n+m){
    79         memset(f,0,sizeof(f));
    80         For(i,0,n)
    81             e[i]=0;
    82 
    83         For(i,1,n){
    84             in(x);in(a[i]);
    85             push(x,i);
    86         }
    87         dfs(0,m+1);
    88         o(f[0][m+1]);p('
    ');
    89     }
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    一些术语
    Professional Frontend Engineering
    爱上阿森纳,爱上一种信仰
    ThinkPHP 和 UCenter接口的冲突
    这个城市
    来自Google的10条价值观
    如何将Gb2312转为unicode?
    未完成的代码(JS)
    微软也用PHP?!
    博客园对"公告"的Js进行了过滤
  • 原文地址:https://www.cnblogs.com/war1111/p/10386887.html
Copyright © 2011-2022 走看看