SPOJ QTree5 [点分治][堆]
来源:互联网 发布:进入淘宝 编辑:程序博客网 时间:2024/06/14 05:30
点分治模板题,我有个max细节没注意到,root一直取错,小数据AC,大数据就TLE了。。。。找了半天才发现。。。
MD
点分治+堆维护路径值(与QTree4很像)。
code
#include<algorithm>#include<cstring>#include<cstdio>#include<cmath>#include<queue>const int N=200220;using namespace std;inline int read(){ static char ch;int flag=1,res=0; while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48; return res*=flag;}int to[N],nxt[N];struct node{ int u,dis; bool operator < (const node &rhs)const{ return dis>rhs.dis; }};priority_queue<node> q[N];int n,tot=1,cnt,x,y,zx,zy,t,tal,dt,root;int deep[N],head[N],sz[N],mx[N],fa[N][20],dis[N][20],tail[N];bool vis[N],col[N];inline void dfs1(int u,int f){ sz[u]=1;for(int i=head[u];i;i=nxt[i]) if(to[i]!=f&&!vis[to[i]])dfs1(to[i],u),sz[u]+=sz[to[i]];}inline void dfs2(int u,int f){ mx[u]=tal-sz[u];for(int i=head[u];i;i=nxt[i]) if(to[i]!=f&&!vis[to[i]])dfs2(to[i],u),mx[u]=max(mx[u],sz[to[i]]); if(mx[u]<dt)dt=mx[u],root=u;}inline void dfs3(int u,int f,int dep,int d){ fa[u][dep]=root,dis[u][dep]=d; for(int i=head[u];i;i=nxt[i]) if(to[i]!=f&&!vis[to[i]])dfs3(to[i],u,dep,d+1);}inline void dfs(int u,int dep){ dfs1(u,0); tal=dt=sz[u];dfs2(u,0); vis[u=root]=true;tail[u]=dep; dfs3(u,0,dep,0); for(int i=head[u];i;i=nxt[i]) if(!vis[to[i]])dfs(to[i],dep+1);}int main(){ n=read(); for(int i=1;i<n;i++){ x=read();y=read(); to[++tot]=y,nxt[tot]=head[x],head[x]=tot, to[++tot]=x,nxt[tot]=head[y],head[y]=tot; } dfs(1,0);n=read();zx=0; while(n--){ int i; if(read()){ y=read(); if(zx){ zy=1<<30; for(i=tail[y];i>=0;i--){ t=fa[y][i]; while(!q[t].empty()&&!col[q[t].top().u])q[t].pop(); if(!q[t].empty())zy=min(zy,q[t].top().dis+dis[y][i]); } printf("%d\n",zy); }else puts("-1"); }else if(col[y=read()]^=1)for(i=tail[y],zx++;i>=0;i--)q[fa[y][i]].push((node){y,dis[y][i]});else zx--; } return 0;}
阅读全文
0 0
- SPOJ QTree5 [点分治][堆]
- SPOJ QTREE5 lct
- SPOJ QTree5 [树链剖分]
- SPOJ Free tour II 点分治
- spoj 1825 Free tour II 点分治
- 【SPOJ】1825 Free tour II 点分治
- SPOJ QTREE4 Query on a tree IV (边分治 + 堆)
- SPOJ 2939 QTREE5 - Query on a tree V
- spoj 1825 Free tour II(树的点分治)
- SPOJ 1825 FTOUR2 - Free tour II (树上点分治)
- Spoj - 1825 Free tour II(树的点分治)
- bzoj 3784(点分治+RMQ+堆)
- spoj qtree4 边分治
- bzoj1095 Hide 捉迷藏 动态点分治+堆 (附动态点分治详解)
- BZOJ 3784|树上的路径|点分治|堆|RMQ
- 【bzoj3784】【树上的路径】【点分治+堆+st表】
- [BZOJ 3784][树上的路径][点分治+堆]
- bzoj2006 [ NOI2010 ] && bzoj3784 --点分治+线段树+堆
- 网络流系列
- 学习Java有没有什么捷径
- 关于Make File 看了这两篇博客就足够了!
- Android 版本更新
- 英文文献及论文写作中的一些技巧
- SPOJ QTree5 [点分治][堆]
- BZOJ 4872 [Shoi2017] 分手是祝愿
- jedis连接linux集群失败,单个可以连接
- 【NOIP2013模拟11.7B组】道路覆盖(cover)
- 使用Nginx实现反向代理
- c3p0连接池
- JZOJ1297.【USACO题库】4.1.1 Beef McNuggets麦香牛块
- JAVAWEB开发之redis学习(九)——redis主从复制入门及原理
- Android 水平方向跑马灯例子