[POI2014][BZOJ3522/4543]Hotel/[JZOJ5357]校门外的树
来源:互联网 发布:硬盘损坏数据恢复 编辑:程序博客网 时间:2024/06/05 18:41
题目大意
给定一棵
题目分析
考虑
令
然后在
对于一个子树
这样我们就可以做到
怎么将时空复杂度优化呢?注意到对于节点
我们不妨对这棵树长链剖分,然后将重儿子作为第一次枚举的子树,使用类似指针的思路来做到
这样做时间复杂度是
至于空间复杂度,我们给每条长链顶端分配正比于长链长度的空间就好了,最后也是
一些实现细节请读者自行思考。
代码实现
#include <iostream>#include <cstdio>#include <cctype>using namespace std;typedef long long LL;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=100005;const int M=N<<1;const int E=N<<1;int last[N],hea[N],depth[N],lng[N],mxl[N],fa[N],fst[N],gst[N];int tov[E],nxt[E];LL f[N],g[M];int n,tot,fcnt,gcnt;LL ans;void insert(int x,int y){tov[++tot]=y,nxt[tot]=last[x],last[x]=tot;}void dfs(int x){ mxl[x]=lng[x]=0; for (int i=last[x],y;i;i=nxt[i]) if ((y=tov[i])!=fa[x]) { depth[y]=depth[fa[y]=x]+1,dfs(y); if (mxl[x]<mxl[y]+1) mxl[x]=mxl[lng[x]=y]+1; }}void dp(int x,int top){ if (x==top) fst[x]=fcnt,fcnt+=mxl[x]+1,gst[x]=gcnt,gcnt+=mxl[x]<<1|1; if (lng[x]) dp(lng[x],top); int fptr=fst[top]+depth[x]-depth[top],gptr=gst[top]+mxl[top]-depth[x]+depth[top]; ++f[fptr],ans+=g[gptr]; for (int i=last[x],y;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&y!=lng[x]) { dp(y,y); for (int j=0;j<mxl[y];++j) ans+=f[fptr+j]*g[gst[y]+mxl[y]+j+1]; for (int j=0;j<=mxl[y]&&gptr+j+1<gst[top]+(mxl[top]<<1|1);++j) ans+=g[gptr+j+1]*f[fst[y]+j]; for (int j=0;j<=mxl[y]&&gptr+j+1<gst[top]+(mxl[top]<<1|1)&&fptr+j+1<fst[top]+mxl[top]+1;++j) g[gptr+j+1]+=f[fptr+j+1]*f[fst[y]+j]; for (int j=0;j<mxl[y];++j) g[gptr+j]+=g[gst[y]+mxl[y]+j+1]; for (int j=0;j<=mxl[y]&&fptr+j+1<fst[top]+mxl[top]+1;++j) f[fptr+j+1]+=f[fst[y]+j]; }}int main(){ freopen("tree.in","r",stdin),freopen("tree.out","w",stdout); n=read(); for (int i=1,x,y;i<n;++i) x=read(),y=read(),insert(x,y),insert(y,x); depth[1]=1,dfs(1),fcnt=gcnt=1,dp(1,1),printf("%lld\n",ans); fclose(stdin),fclose(stdout); return 0;}
阅读全文
0 0
- [POI2014][BZOJ3522/4543]Hotel/[JZOJ5357]校门外的树
- BZOJ3522&4543: [POI2014]Hotel加强版
- [BZOJ3522] [Poi2014]Hotel
- 【BZOJ3522】[Poi2014]Hotel【DFS】
- BZOJ3522: [Poi2014]Hotel Dfs
- 【bzoj3522】[Poi2014]Hotel
- 【bzoj3522】 [Poi2014]Hotel
- [bzoj3522][bzoj4543][POI2014]HOTEL
- BZOJ3522 [Poi2014]Hotel
- [杂题] BZOJ3522: [Poi2014]Hotel
- 【bzoj3522】[Poi2014]Hotel
- Bzoj3522:[Poi2014]Hotel:树形DP:
- 【bzoj3522】【poi2014】【hotel】【树形dp】
- BZOJ3522——[Poi2014]Hotel
- 【bzoj3522】[Poi2014]Hotel 暴力+计数
- [bzoj3522][Poi2014]Hotel(暴力)
- 【bzoj3522】[Poi2014]Hotel 树形dp
- BZOJ4543/BZOJ3522 [POI2014]Hotel加强版
- 当你在设置里修改字体大小的时候,到底在修改什么
- 机器学习学习笔记.day4
- RecycleView的分割线类
- JBOSS简介及安装部署
- remove-element
- [POI2014][BZOJ3522/4543]Hotel/[JZOJ5357]校门外的树
- 课后练习
- vacuumdb -d test -f -z -v输出结果
- No service of type Factory available in ProjectScopeServices
- Apache服务器的性能测试工具ab 、http_load 日志分析工具webalizer
- Python学习笔记(二):if语句的使用
- Linux命令学习20170911
- 实现在html上点击一个按钮出现一张照片
- 123