[SDOI2017]树点涂色
来源:互联网 发布:软件应用商业计划书 编辑:程序博客网 时间:2024/06/06 03:38
[SDOI2017]树点涂色
Description
Bob有一棵n个点的有根树,其中1号点是根节点。Bob在每个点上涂了颜色,并且每个点上的颜色不同。定义一条路
径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色。Bob可能会进行这几种操作:
1 x:
把点x到根节点的路径上所有的点染上一种没有用过的新颜色。
2 x y:
求x到y的路径的权值。
3 x y:
在以x为根的子树中选择一个点,使得这个点到根节点的路径权值最大,求最大权值。
Bob一共会进行m次操作
Input
第一行两个数n,m。
接下来n-1行,每行两个数a,b,表示a与b之间有一条边。
接下来m行,表示操作,格式见题目描述
1<=n,m<=100000
Output
每当出现2,3操作,输出一行。
如果是2操作,输出一个数表示路径的权值
如果是3操作,输出一个数表示权值的最大值
Sample Input
5 6
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5
Sample Output
3
4
2
2
Time Limit: 10 Sec Memory Limit: 128 MB
题解:
操作一就是简单的lct的access操作了,所以直接用access维护。
操作二就是看要查询的节点到根节点虚边的个数就可以了。
操作三查询以一节点为跟的最大值,考虑如何维护最大值。经画图可以发现,在access时,在以所断的连 边的节点为根节点的子树+1,所连的子树权值-1,用线段树维护max就好了。
代码:
#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int max_n = 100001;const int inf = 1e9+7;struct node{ int num; node *ch[2]; node *pre; void set_son(node *son,int wh); int get_wh(){return pre->ch[0]==this ? 0 : 1;} int is_root();}pool[max_n],*root,*null;inline int node::is_root(){ if(pre==null) return 1; return !(pre->ch[0]==this || pre->ch[1]==this);}inline void node::set_son(node *son,int wh){ ch[wh]=son; if(son!=null) son->pre=this;}struct lnode{ int num,max,l,r,size; int delta;}tree[max_n*4];int point[max_n*2],nxt[max_n*2],v[max_n*2];int deep[max_n],rank_n[max_n],end_n[max_n],top[max_n],size[max_n],fa[max_n];int n,m,a,b,c,x,y,tot,num;inline node *newnode(){ node *now=pool+ ++num; now->num=num; now->ch[0]=now->ch[1]=now->pre=null; return now;}inline void init(){ memset(point,-1,sizeof(point)); memset(nxt,-1,sizeof(nxt)); tot=0; num=0; null=pool; null->num=0; null->ch[0]=null->ch[1]=null->pre=null; root=null; for(int i=1; i<=n; ++i) newnode();} inline void update(int now){ tree[now].max=max(tree[now<<1].max,tree[(now<<1)+1].max); tree[now].size=tree[now<<1].size+tree[(now<<1)+1].size; tree[now].num=tree[now<<1].num+tree[(now<<1)+1].num;}inline void pushdown(int now){ if(tree[now].delta) { tree[now<<1].num+=tree[now].delta*tree[now<<1].size; tree[now<<1].max+=tree[now].delta; tree[now<<1].delta+=tree[now].delta; tree[(now<<1)+1].num+=tree[now].delta*tree[(now<<1)+1].size; tree[(now<<1)+1].max+=tree[now].delta; tree[(now<<1)+1].delta+=tree[now].delta; update(now); tree[now].delta=0; }}inline void addedge(int x,int y){ ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;}inline void rotate(node *now){ node *f=now->pre,*grand=now->pre->pre; int wh=now->get_wh(); f->set_son(now->ch[wh^1],wh); if(f->is_root()) now->pre=grand; else grand->set_son(now,f->get_wh()); now->set_son(f,wh^1);}inline void splay(node *now){ if(now->is_root()) return; for(; !now->is_root(); rotate(now)) if(!now->pre->is_root()) now->pre->get_wh()==now->get_wh() ? rotate(now->pre) : rotate(now);}inline void build(int now,int l,int r){ tree[now].l=l; tree[now].r=r; if(l==r) { tree[now].size=1; return; } int mid=(l+r)>>1; build(now<<1,l,mid); build((now<<1)+1,mid+1,r); update(now);}inline void change(int now,int l,int r,int val){ if(l==0 || r==0) return; int lr=tree[now].l,rr=tree[now].r; if(l<=lr && rr<=r) { tree[now].max+=val; tree[now].delta+=val; tree[now].num+=val*tree[now].size; return; } pushdown(now); int mid=(lr+rr)>>1; if(l<=mid) change(now<<1,l,r,val); if(r>mid) change((now<<1)+1,l,r,val); update(now);}inline int query_max(int now,int l,int r){ int lr=tree[now].l,rr=tree[now].r; if(l<=lr && rr<=r) return tree[now].max; int mid=(lr+rr)>>1,ans=-inf; pushdown(now); if(l<=mid) ans=max(ans,query_max(now<<1,l,r)); if(r>mid) ans=max(ans,query_max((now<<1)+1,l,r)); return ans;}inline int squery(int x,int y){ while(top[x]!=top[y]) { if(deep[top[x]]<deep[top[y]]) swap(x,y); x=fa[top[x]]; } if(rank_n[x]>rank_n[y]) swap(x,y); return x;}inline void dfs1(int now,int f){ fa[now]=f; (pool+now)->pre=(pool+f); deep[now]=deep[f]+1; size[now]=1; for(int i=point[now]; i!=-1; i=nxt[i]) if(v[i]!=f) { dfs1(v[i],now); size[now]+=size[v[i]]; }}inline void dfs2(int now,int tip){ top[now]=tip; rank_n[now]=++tot; change(1,tot,tot,deep[now]); if(now!=1 && nxt[point[now]]==-1) { end_n[now]=now; return; } int mson=0; for(int i=point[now]; i!=-1; i=nxt[i]) if(size[v[i]]<size[now] && size[v[i]]>size[mson]) mson=v[i]; dfs2(mson,tip); end_n[now]=end_n[mson]; for(int i=point[now]; i!=-1; i=nxt[i]) if(size[v[i]]<size[now] && v[i]!=mson) { dfs2(v[i],v[i]); end_n[now]=end_n[v[i]]; }}inline int find_min(node *now){ while(now->ch[0]!=null) now=now->ch[0]; return now->num;}inline node *access(node *now){ node *y=null; for(; now!=null; y=now,now=now->pre) { splay(now); if(now->ch[1]!=null) change(1,rank_n[find_min(now->ch[1])],rank_n[end_n[find_min(now->ch[1])]],1); if(y!=null) change(1,rank_n[find_min(y)],rank_n[end_n[find_min(y)]],-1); now->set_son(y,1); } return y;}inline int work(int x){ node *now=pool+x; int ans=0; while(now!=null) { splay(now); now=now->pre; ans++; } return ans;}inline int read(){ int ans=0;char c;bool flag=false; while( (c=getchar())==' ' || c=='\n' || c=='\r'); if(c=='-') flag=true;else ans=c-'0'; while((c=getchar()) >='0' && c<='9') ans=ans*10+c-'0'; return ans*(flag?-1:1);} int main(){ scanf("%d%d",&n,&m); init(); for(int i=1; i<n; ++i) { a=read(); b=read(); addedge(a,b); } tot=0; build(1,1,n); dfs1(1,0); dfs2(1,1); for(int i=1; i<=m; ++i) { scanf("%d",&c); if(c==1) { x=read(); access(pool+x); } if(c==2) { x=read(); y=read(); printf("%d\n",work(x)+work(y)-2*work(squery(x,y))+1); } if(c==3) { x=read(); printf("%d\n",query_max(1,rank_n[x],rank_n[end_n[x]])); } } return 0;}
阅读全文
0 0
- [SDOI2017]树点涂色
- [SDOI2017]树点涂色
- BZOJ4817 [Sdoi2017]树点涂色
- 4817: [Sdoi2017]树点涂色
- BZOJ4817: [Sdoi2017]树点涂色
- bzoj4817 [Sdoi2017]树点涂色
- bzoj 4817: [Sdoi2017]树点涂色
- [LCT] BZOJ 4817 [Sdoi2017]树点涂色
- [LCT] BZOJ4817.[Sdoi2017]树点涂色
- loj #2001. 「SDOI2017」树点涂色(LCT)
- bzoj 4817: [Sdoi2017]树点涂色 link cut tree+线段树+树链剖分
- [BZOJ4817][SDOI2017]树点涂色(DFS序+LCA+树剖+LCT)
- 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
- poj2777线段树+涂色问题
- [线段树] BZOJ 4821 [Sdoi2017]相关分析
- [BZOJ4821][SDOI2017]相关分析(线段树)
- [BZOJ]4821: [Sdoi2017]相关分析 线段树
- bzoj 4821: [Sdoi2017]相关分析 线段树
- 关于StringIndexOutOfBoundsException那些事~
- Android动画篇(三):canvas.clipRect 纠错篇
- Codeforces Round #423 Di v. 2(这可能是我写的最认真的一套题了,接下来会继续的)
- Android--广播BroadcastReceiver
- mybatis的嵌套查询和延迟加载分析
- [SDOI2017]树点涂色
- leetcode traning_string(2)
- 邂逅StringIndexOutOfBoundsException
- AES加解密int数组——Java C++实现
- android几个自定义广播和系统广播的用法
- mybatis的基本使用及配置
- c/c++中关于qsort的使用
- 异步加载js不允许使用document.write的解决方法
- 相对布局解析以及Android studio 连接夜神模拟器