贪心——BZOJ4813/Luogu3698 [CQOI2017]小Q的棋盘
来源:互联网 发布:手机手柄映射软件 编辑:程序博客网 时间:2024/05/16 07:05
题面:BZOJ4813 Luogu3698
一个很好想的贪心
注意到这个图是一棵树,所以我们可以直接往树的深度最深处一直下去(注意是不回来了的),这样可以保证浪费是最少的
然后呢如果沿着这条链一直下去把n的步数用完了,那点数就是m+1(还有根节点)
如果没用完(这条路径到头了但是还有剩余步数),那就把剩余的步数全部除以2(一条路径走两次,为什么自己yy),加上原来答案就可以了
这样生成的答案它的路径是这样的:根 -> 乱七八糟的点(要回来) ->最长链(不回来了,终点就在这上面)
这样可以证明是最优的
吐槽一下数据,一开始交的贪心有Bug居然给我过掉了。。。
没有Bug的代码:
#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<iostream>#include<cstdlib>#include<queue>#include<string>#include<ctime>#include<climits>using namespace std;int nedge=0,p[100001],nex[100001],head[100001];bool vis[10001];int n,m,ans;inline void addedge(int a,int b){ p[++nedge]=b;nex[nedge]=head[a];head[a]=nedge;}inline void dfs(int x,int v){ ans=max(ans,v);vis[x]=1; for(int k=head[x];k;k=nex[k])if(!vis[p[k]])dfs(p[k],v+1);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<n;i++){ int x,y;scanf("%d%d",&x,&y); addedge(x,y);addedge(y,x); } dfs(0,1); if(ans>m)return printf("%d",m+1)&0; int r=m-(ans-1); if(ans+r/2>n)printf("%d",n); else printf("%d",ans+r/2); return 0;}
阅读全文
1 0
- 贪心——BZOJ4813/Luogu3698 [CQOI2017]小Q的棋盘
- 【BZOJ4813】小Q的棋盘(CQOI2017)-贪心+DFS
- 【CQOI2017】bzoj4813 小Q的棋盘
- BZOJ4813: [Cqoi2017]小Q的棋盘
- BZOJ4813 [Cqoi2017]小Q的棋盘
- bzoj4813: [Cqoi2017]小Q的棋盘
- 【BZOJ4813】 [Cqoi2017]小Q的棋盘
- bzoj4813 [Cqoi2017]小Q的棋盘 树上DP
- cqoi2017,bzoj4813小Q的棋盘(树形dp或瞎搞)
- 【CQOI2017】小Q的棋盘
- CQOI2017 小Q的棋盘
- [bzoj4813][树形dp]小Q的棋盘
- bzoj 4813 [Cqoi2017]小Q的棋盘
- BZOJ 4813 [Cqoi2017]小Q的棋盘
- 4813: [Cqoi2017]小Q的棋盘
- BZOJ 4813 [Cqoi2017]小Q的棋盘
- 4813: [Cqoi2017]小Q的棋盘
- [BZOJ 4813][Cqoi2017]小Q的棋盘:TreeDP
- 关于使用c3p0乱码问题
- java注解
- oracle控制文件
- winform的微信扫描支付
- oracle管理重做日志文件
- 贪心——BZOJ4813/Luogu3698 [CQOI2017]小Q的棋盘
- SpringMVC自定义拦截器
- oracle创建表空间
- POJ 1320 Street Numbers 笔记
- BZOJ 3931: [CQOI2015]网络吞吐量 dijstra+heap+最大流
- 自制板用USBTinyISP刷机失败率高,是因为驱动的关系?
- oracle数据文件管理
- html表单中插入图形
- java 继承中的方法重写