bzoj1023 仙人掌图 树形dp
2015-12-27 20:23
302 查看
仙人掌图的入门题(以及被入门题虐哭的我T_T),膜拜了ydc的题解,讲一下自己的看法。
在dfs树中,如果令f[x]表示x到它的叶子节点的路径中最长路径的长度,则有递推式f[x]=max{f[son]+1}。
那么我们首先考虑桥带来的影响,在以x为更节点的子树中,如果(x,y)和(x,z)都是桥且f[y]最大,f[z]其次,则答案可以更新为f[y]+f[z]+2。那么我们可以这样更新,对于桥(x,y),它对答案的贡献为f[x]+f[y]+1,即一条从一个子节点->v->u到另一个子节点的路径。更新完答案后,用f[y]尝试更新f[x]。那么会不会出现在f[y]的max值出现而f[z]的max而没有出现等类似这种使得我们错过最有答案呢?不会。如果最大的f[y]先出现,那么f[x]首先被更新为f[y]+1,那么等f[z]出现时对答案的贡献为f[x]+f[z]+1=f[y]+f[x]+2。同理f[z]先出现也是如此。
现在考虑环带来的影响。在一个环中,定义x为环中在dfs中最高的点。那么只要得出来f[x],就可以更新这个环上面(或者说x的祖先)的点,而环中的其它点对于这个环上面的点并没有任何影响,这是我们时间复杂度的保证。如果令dep[k]表示任意一个点k的深度,令dist(x,y)表示x到y的最短路径,那么f[x]=max{f[y],dist(x,y)}。同时我们还要考虑环对答案的影响。对于一个点i,对答案的影响为max{f[i]+f[j]+i-j},其中j<i,那么可以用单调队列维护f[j]-j,时间就降到O(环中点的个数),均摊到整个图就变为O(N)(注意每个环只“操作”一次“)。
AC代码如下:
by lych
2015.12.27
在dfs树中,如果令f[x]表示x到它的叶子节点的路径中最长路径的长度,则有递推式f[x]=max{f[son]+1}。
那么我们首先考虑桥带来的影响,在以x为更节点的子树中,如果(x,y)和(x,z)都是桥且f[y]最大,f[z]其次,则答案可以更新为f[y]+f[z]+2。那么我们可以这样更新,对于桥(x,y),它对答案的贡献为f[x]+f[y]+1,即一条从一个子节点->v->u到另一个子节点的路径。更新完答案后,用f[y]尝试更新f[x]。那么会不会出现在f[y]的max值出现而f[z]的max而没有出现等类似这种使得我们错过最有答案呢?不会。如果最大的f[y]先出现,那么f[x]首先被更新为f[y]+1,那么等f[z]出现时对答案的贡献为f[x]+f[z]+1=f[y]+f[x]+2。同理f[z]先出现也是如此。
现在考虑环带来的影响。在一个环中,定义x为环中在dfs中最高的点。那么只要得出来f[x],就可以更新这个环上面(或者说x的祖先)的点,而环中的其它点对于这个环上面的点并没有任何影响,这是我们时间复杂度的保证。如果令dep[k]表示任意一个点k的深度,令dist(x,y)表示x到y的最短路径,那么f[x]=max{f[y],dist(x,y)}。同时我们还要考虑环对答案的影响。对于一个点i,对答案的影响为max{f[i]+f[j]+i-j},其中j<i,那么可以用单调队列维护f[j]-j,时间就降到O(环中点的个数),均摊到整个图就变为O(N)(注意每个环只“操作”一次“)。
AC代码如下:
#include<iostream> #include<cstdio> #include<cstring> #define N 500005 using namespace std; int n,m,ans,tot,dfsclk,fst ,pnt[N<<2],nxt[N<<2],f ,fa ,a ,dep ,pos ,low ,q ; int read(){ int x=0; char ch=getchar(); while (ch<'0' || ch>'9') ch=getchar(); while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x; } void add(int aa,int bb){ pnt[++tot]=bb; nxt[tot]=fst[aa]; fst[aa]=tot; } void solve(int x,int y){ int cnt=dep[y]-dep[x]+1,head=1,tail=1,i; for (i=y; i!=x; i=fa[i]) a[cnt--]=f[i]; a[1]=f[x]; cnt=dep[y]-dep[x]+1; q[1]=1; for (i=1; i<=cnt; i++) a[i+cnt]=a[i]; for (i=2; i<=cnt+(cnt>>1); i++){ if (i-q[head]>(cnt>>1)) head++; ans=max(ans,a[i]+i+a[q[head]]-q[head]); while (head<=tail && a[i]-i>=a[q[tail]]-q[tail]) tail--; q[++tail]=i; } for (i=2; i<=cnt; i++) f[x]=max(f[x],a[i]+min(i-1,cnt-i+1)); } void dfs(int x){ pos[x]=low[x]=++dfsclk; int p; for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; if (y==fa[x]) continue; if (!pos[y]){ fa[y]=x; dep[y]=dep[x]+1; dfs(y); } low[x]=min(low[x],low[y]); if (low[y]>pos[x]){ ans=max(ans,f[y]+f[x]+1); f[x]=max(f[x],f[y]+1); } } for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; if (fa[y]!=x && pos[x]<pos[y]) solve(x,y); } } int main(){ n=read(); m=read(); int i; for (i=1; i<=m; i++){ int tmp=read(),last=0; while (tmp--){ int x=read(); if (last){ add(x,last); add(last,x); } last=x; } } dfs(1); printf("%d\n",ans); return 0; }
by lych
2015.12.27
相关文章推荐
- 腾讯网迷你版如何关闭
- Rung-Kutta解线性常微分方程组
- 关于字符的一些看法
- bzoj3230: 相似子串
- Json在线工具使用说明
- 如何在Fedora 17 18 19 20 21 22 23 系统中安装 openjdk 1.6 与 1.7 1.8 共存
- python的模块安装
- linux动态库和静态库
- Genymotion error:The virtual device got no IP address
- CentOS/Linux 网卡设置 IP地址配置永久生效
- Dreamweaver 中文乱码
- iOS界面设计指南
- Hibernate映射问题
- Centos6.7搭建Rsyslog日志服务器
- 中断处理
- SpringMVC返回数据方式
- dom高级程序设计学习
- 02.cocos2d-x触摸事件(一)
- 检测主机是否在线小脚本
- 关于嵌套图片标签的问题