bzoj2525 1426
来源:互联网 发布:android 网络测速代码 编辑:程序博客网 时间:2024/05/15 05:37
题面二分最短时间,求出最小的需要引爆数
至于关键点的引爆状态有关
f[i] 以i为根的子树中已经引爆的点离i最近的距离
g[i] 以i为根的子树中未引爆的点离i最远的距离
回溯到每个节点时,优先考虑用另一个儿子中的点覆盖其他儿子
if(f[i]+g[i]<=mid) g[i]=INF
if(g[i]==mid) 必须引爆x
#include <iostream>#include <cstdio>#include <cstring>#define INF 1000000000#define maxn 300005using namespace std;int n,m;int a[maxn];int ans;struct edge{ int to,ne;}b[maxn*2];int k=0,head[maxn];int f[maxn],g[maxn];int limit;int num=0,op1=0;inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f;}void add(int u,int v){ k++; b[k].to=v; b[k].ne=head[u]; head[u]=k;}void dfs(int x,int pre){ f[x]=INF; if(a[x]) g[x]=0; else g[x]=-INF; for(int i=head[x];i!=-1;i=b[i].ne) if(b[i].to!=pre){ dfs(b[i].to,x); f[x]=min(f[x],f[b[i].to]+1); g[x]=max(g[x],g[b[i].to]+1); } if(g[x]+f[x]<=limit) g[x]=-INF; if(g[x]==limit){ f[x]=0,g[x]=-INF,num++; }}bool check(int x){ if(!x) return op1<=m; limit=x; num=0; dfs(1,0); //printf("%d %d %d %d\n",x,f[1],g[1],num); if(g[1]+f[1]>limit) num++; return num<=m;}void getans(){ int l=0,r=n,mid; ans=n; while(l<=r){ mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; }}int main(){ //freopen("in.txt","r",stdin); memset(head,-1,sizeof(head)); n=read(); m=read(); for(int i=1;i<=n;i++){ a[i]=read(); if(a[i]) op1++; } int x,y; for(int i=1;i<n;i++) { x=read(); y=read(); add(x,y); add(y,x); } getans(); printf("%d\n",ans); //while(1); return 0;}
题面
考虑逆推
f[i] 已经买了i张邮票,距离n张邮票还需要的购买次数
g[i]表示所需钱数
假设每张邮票需要一元,后面购买的邮票都会贵1元
阅读全文
0 0
- bzoj2525 1426
- BZOJ2525: [Poi2011]Dynamite
- BZOJ2525: [Poi2011]Dynamite
- BZOJ2525: [Poi2011]Dynamite
- [BZOJ2525][Poi2011]Dynamite 二分答案+贪心+树型DP
- 1426
- poj 1426
- sicily 1426
- poj 1426
- POJ 1426
- POJ 1426
- sicily 1426
- poj 1426
- poj 1426
- poj 1426
- POJ 1426
- poj-1426
- poj 1426
- CMU统计机器学习2017 春季课程:研二水平
- 中美争霸人工智能背后,是不甘落后的加拿大 | 现场报道
- 一篇新的Capsule论文:优于基准CNN(ICLR 2018盲审中)
- Array和ArrayList区别
- 吴恩达deeplearning.ai课程系列笔记07
- bzoj2525 1426
- 字体属性设置(一):谷歌浏览器12px以下字体的显示;方法和原理
- Mysql和MongoDB常用操作总结
- int,long long,double,float等的范围(和精度)
- 有这三种思想的人永远做不了老板
- Xcode9学习笔记20
- 【洛谷 P2656】采蘑菇
- 洛谷P1052 过河(NOIp2005)
- 交换两个变量