bzoj4027 贪心
2015-12-28 22:33
405 查看
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=4027
大意:给一棵有根树,当你删除一个节点时,这个点上的权值会加到父节点,儿子连到父节点上。要求一个点上的权值+儿子个数<=m。根节点不能删。
贪心:设删点对父节点的影响为g。对一个子树,肯定尽可能多的删子树内的点。(删子树根节点的话只能删一个点且最后会删到根节点)
大意:给一棵有根树,当你删除一个节点时,这个点上的权值会加到父节点,儿子连到父节点上。要求一个点上的权值+儿子个数<=m。根节点不能删。
贪心:设删点对父节点的影响为g。对一个子树,肯定尽可能多的删子树内的点。(删子树根节点的话只能删一个点且最后会删到根节点)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct sef { int ne,en; }h[2000005]; int tot,first[2000005]; int q[2000005],ql; int ans,n,V[2000005],m; inline void setl(int x,int y) { h[++tot].ne=first[x]; h[tot].en=y; first[x]=tot; } void Go(int x) { int y; int ql=0; for (int i=first[x];y=h[i].en,i;i=h[i].ne) { Go(y); ++V[x]; } for (int i=first[x];y=h[i].en,i;i=h[i].ne) q[++ql]=V[y]-1; sort(q+1,q+ql+1); for (int i=1;i<=ql;++i) if (V[x]+q[i]<=m) V[x]+=q[i],++ans; else break; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;++i) scanf("%d",V+i); int k,x; for (int i=1;i<=n;++i) { scanf("%d",&k); while (k--) { scanf("%d",&x); setl(i,++x); } } ans=0; Go(1); printf("%d\n",ans); }
相关文章推荐
- 1.一些 贪心算法 的简单思维题:
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- Jump Game I,II 贪心
- Wildcard Matching
- 贪心法实现无向图的划分 代码
- 贪心题目循环和控制台折行
- HDOJ 1009
- BZOJ3275 Number (最小割)
- 【解题报告】【USACO】酸奶工厂
- 【解题报告】【】交谊舞
- POJ 1328
- Best Cow Line
- POJ2377 Bad Cowtractors
- 贪心 hdu 1003
- 最大容器
- 满足和为定值的两个数或多个数
- Codeforces Round #300
- HUD1052 __ P2 1002 __ Tian Ji -- The Horse Racing
- 2015年国家集训队测试 BZOJ3816矩阵变幻