CF815C Karen and Supermarket

來源:互聯網
上載者:User

標籤:stdin   data   wap   char   space   head   lin   ORC   node   

傳送門

 

題解

 1 //minamoto 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<vector> 6 #define ll long long 7 #define inf 0x3f3f3f3f3f3f3f3f 8 using namespace std; 9 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)10 char buf[1<<21],*p1=buf,*p2=buf;11 int read(){12     #define num ch-‘0‘13     char ch;bool flag=0;int res;14     while(!isdigit(ch=getc()))15     (ch==‘-‘)&&(flag=true);16     for(res=num;isdigit(ch=getc());res=res*10+num);17     (flag)&&(res=-res);18     #undef num19     return res;20 }21 const int N=5005;22 struct node{ll y,n;};vector<node> f[N];23 int head[N],Next[N],ver[N],tot;24 inline void add(int u,int v){25     ver[++tot]=v,Next[tot]=head[u],head[u]=tot;26 }27 int c[N],d[N],sz[N],n,q,m;28 void dfs(int u){29     ll rr,ss,tt;sz[u]=1,f[u].resize(2);30     f[u][0].n=0,f[u][0].y=inf,f[u][1].y=c[u]-d[u],f[u][1].n=c[u];31     for(int i=head[u];i;i=Next[i]){32         int v=ver[i];dfs(v);33         f[u].resize(sz[u]+sz[v]+1,(node){inf,inf});34         for(int j=sz[u];j>=0;--j)35         for(int k=0;k<=sz[v];++k){36             rr=f[u][j].n+f[v][k].n,37             ss=f[u][j].y+f[v][k].n,38             tt=f[u][j].y+f[v][k].y;39             if(rr<=m&&f[u][j+k].n>rr) f[u][j+k].n=rr;40             if(ss<=m&&f[u][j+k].y>ss) f[u][j+k].y=ss;41             if(tt<=m&&f[u][j+k].y>tt) f[u][j+k].y=tt;42         }43         vector<node>().swap(f[v]);44         sz[u]+=sz[v];45     }46 }47 int main(){48 //    freopen("testdata.in","r",stdin);49     n=read(),m=read(),c[1]=read(),d[1]=read();50     for(int i=2,fa;i<=n;++i)51     c[i]=read(),d[i]=read(),fa=read(),add(fa,i);52     dfs(1);53     for(int i=n;i;--i)54     if(f[1][i].y<=m||f[1][i].n<=m) return printf("%d\n",i),0;55     return puts("0"),0;56 }

 

CF815C Karen and Supermarket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.