[CF444E]DZY Loves Planting
来源:互联网 发布:mac如何隐藏菜单栏 编辑:程序博客网 时间:2024/06/05 04:33
题目大意
给定一棵
定义
对于一个序列
现在有
原数据范围
题目分析
首先有个很显然的想法:二分答案,然后用网络流来判定答案是否可行。于是乎好像可以过掉原数据???
来考虑更优的做法:可以发现网络流的过程其实就是一个二分图匹配,考虑怎么把这个问题套上
从小到大加边,然后我们可以把联通块缩起来,这个联通块内所有点都要向联通块外的点匹配才能获得当前边权的答案。根据
直接使用并查集搞一搞就可以了。
时间复杂度
代码实现
#include <algorithm>#include <iostream>#include <cstdio>#include <cctype>using namespace std;int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*f;}const int N=3005;struct edge{ int x,y,len; bool operator<(edge const e){return len<e.len;}}ed[N];int fa[N],rk[N],a[N],size[N];int n,ans,err,sum;int getfather(int son){return fa[son]==son?son:fa[son]=getfather(fa[son]);}void merge(int x,int y){ x=getfather(x),y=getfather(y); if (x==y) return; if (rk[x]<rk[y]) swap(x,y); fa[y]=x,rk[x]+=rk[x]==rk[y]; err-=sum-a[x]<size[x],err-=sum-a[y]<size[y]; size[x]+=size[y],a[x]+=a[y],err+=sum-a[x]<size[x];}void calc(){ err=0; for (int i=1;i<=n;++i) err+=sum-a[i]<(size[i]=1),fa[i]=i,rk[i]=0; for (int cur=1;cur<n;) { if (!err) ans=ed[cur].len; for (int ptr=cur;cur<n&&ed[cur].len==ed[ptr].len;++cur) merge(ed[cur].x,ed[cur].y); }}int main(){ freopen("planting.in","r",stdin),freopen("planting.out","w",stdout); n=read(); for (int i=1;i<n;++i) ed[i].x=read(),ed[i].y=read(),ed[i].len=read(); sort(ed+1,ed+n); for (int i=1;i<=n;++i) sum+=a[i]=read(); calc(),printf("%d\n",ans); fclose(stdin),fclose(stdout); return 0;}
阅读全文
0 0
- [CF444E]DZY Loves Planting
- Codeforces444E DZY Loves Planting
- cf445A DZY Loves Chessboard
- cf445B DZY Loves Chemistry
- cf444A DZY Loves Physics
- cf446A DZY Loves Sequences
- C. DZY Loves Sequences
- D. DZY Loves Modification
- DZY Loves Fibonacci Numbers
- 【CF】DZY Loves Chessboard
- A. DZY Loves Hash
- B. DZY Loves Strings
- C. DZY Loves Sequences
- A. DZY Loves Hash
- B. DZY Loves Strings
- DZY Loves Chessboard
- A. DZY Loves Chessboard
- dzy loves chessboard
- Spring事务传播性与隔离级别
- Android Hierarchy Viewer位置
- mycat读写分离
- 阿里云ECS CentOS 7 安装图形化桌面
- Qt-textBrowser自动滚动到底部
- [CF444E]DZY Loves Planting
- mongodb整合入spring
- 线性表链式存储及其实现(单链表,循环链表,双向链表)
- Spring Boot 学习第三步(Spring Data JPA学习汇总)
- kotlin Unresolved reference: kotlinx
- Java基础之方法-[Android_YangKe]
- iot小能手:【GAgent+STM32+UCOSIII】之智能贴锁
- 职工请事假 单位有权不批准
- git管理:github管理神器SourceTree