• 【51nod】猴猴吃苹果【树链剖分】


    题目大意:

    题目链接:https://www.51nod.com/Contest/Problem.html#contestProblemId=1148
    猴猴最喜欢在树上玩耍,一天猴猴又跳上了一棵树,这棵树有N个苹果,每个苹果有一个编号,分别为0~N-1,它们之间由N-1个树枝相连,猴猴可以从树枝的一端爬到树枝的另一端,所以猴猴可以从任意一个苹果的位置出发爬到任意猴猴想去的苹果的位置。猴猴开始在编号为K的苹果的位置,并且把这个苹果吃了,之后每一天猴猴都要去吃一个苹果,但是树上那么多苹果吃哪个呢?猴猴想到自己去吃苹果时一定会把路上遇到的苹果都吃掉,于是猴猴决定去吃能让自己这天吃的苹果数量最多的那个苹果,如果有多个苹果满足条件,猴猴就会去吃这些中编号最小的苹果,那么猴猴会按照什么顺序吃苹果呢?


    思路:

    这道题有dfsdfs的解法。我真是遇树就剖绝了。
    线段树记录每一个节点到根节点的距离dep[i]dep[i],每次选取depdep最大点xx的来转移。然后把rootrootxx的路径上所有点pp的子树的depdep全部减一,因为这个子树中每一个点到根的距离都相当于只要到pp即可。
    拿样例来说
    在这里插入图片描述
    黄色点是树根,蓝色点是dep=1dep=1的点,绿色点是dep=2dep=2的点。
    那么depdep最大并且编号最小的点是0。
    我们把rootroot到0的每一个节点的子树的depdep减一。并输出0
    那么这棵树就变成了
    在这里插入图片描述
    同理,此时我们输出6
    然后又变成了
    在这里插入图片描述
    然后明显3,5顺序输出了。
    所以每一个点的子树只要剖一次,时间复杂度O(nlog2n)O(nlog^2 n)


    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int N=50010;
    int n,root,tot,son[N],fa[N],top[N],size[N],dep[N],id[N],rk[N],head[N];
    bool flag[N];
    
    struct edge
    {
    	int next,to;
    }e[N*2];
    
    struct Treenode
    {
    	int l,r,maxn,pos,lazy;
    };
    
    struct Tree
    {
    	Treenode tree[N*4];
    	int tot;
    	
    	void pushup(int x)
    	{
    		if (tree[x*2].maxn>tree[x*2+1].maxn)
    		{
    			tree[x].maxn=tree[x*2].maxn;
    			tree[x].pos=tree[x*2].pos;
    		}
    		else if (tree[x*2].maxn<tree[x*2+1].maxn)
    		{
    			tree[x].maxn=tree[x*2+1].maxn;
    			tree[x].pos=tree[x*2+1].pos;
    		}
    		else if (tree[x*2].maxn==tree[x*2+1].maxn && tree[x*2].pos<tree[x*2+1].pos)
    		{
    			tree[x].maxn=tree[x*2].maxn;
    			tree[x].pos=tree[x*2].pos;
    		}
    		else
    		{
    			tree[x].maxn=tree[x*2+1].maxn;
    			tree[x].pos=tree[x*2+1].pos;
    		}
    	}
    	
    	void pushdown(int x)
    	{
    		if (tree[x].lazy)
    		{
    			tree[x*2].lazy+=tree[x].lazy;
    			tree[x*2+1].lazy+=tree[x].lazy;
    			tree[x*2].maxn+=tree[x].lazy;
    			tree[x*2+1].maxn+=tree[x].lazy;
    			tree[x].lazy=0;
    		}
    	}
    	
    	void build(int x,int l,int r)
    	{
    		tree[x].l=l; tree[x].r=r;
    		if (l==r)
    		{
    			tree[x].maxn=dep[rk[l]];
    			tree[x].pos=rk[l];
    			return;
    		}
    		int mid=(l+r)>>1;
    		build(x*2,l,mid); build(x*2+1,mid+1,r);
    		pushup(x);
    	}
    	
    	void update(int x,int l,int r)
    	{
    		if (tree[x].l==l && tree[x].r==r)
    		{
    			tree[x].lazy--;
    			tree[x].maxn--;
    			return;
    		}
    		pushdown(x);
    		int mid=(tree[x].l+tree[x].r)>>1;
    		if (r<=mid) update(x*2,l,r);
    		else if (l>mid) update(x*2+1,l,r);
    		else update(x*2,l,mid),update(x*2+1,mid+1,r);
    		pushup(x);
    	}
    }Tree;
    
    void add(int from,int to)
    {
    	e[++tot].to=to;
    	e[tot].next=head[from];
    	head[from]=tot;
    }
    
    void dfs1(int x,int f)
    {
    	dep[x]=dep[f]+1; size[x]=1; fa[x]=f;
    	for (int i=head[x];~i;i=e[i].next)
    	{
    		int v=e[i].to;
    		if (v!=f)
    		{
    			dfs1(v,x);
    			size[x]+=size[v];
    			if (size[v]>size[son[x]]) son[x]=v;
    		}
    	}
    }
    
    void dfs2(int x,int tp)
    {
    	top[x]=tp; id[x]=++tot; rk[tot]=x;
    	for (int i=head[x];~i;i=e[i].next)
    		if (e[i].to==son[x]) dfs2(e[i].to,tp);
    	for (int i=head[x];~i;i=e[i].next)
    	{
    		int v=e[i].to;
    		if (v!=son[x] && v!=fa[x]) dfs2(v,v);
    	}
    }
    
    int main()
    {
    	memset(head,-1,sizeof(head));
    	scanf("%d%d",&n,&root);
    	root++;
    	for (int i=1,x;i<n;i++)
    	{
    		scanf("%d",&x);
    		add(x+1,i+1); add(i+1,x+1);
    	}
    	dep[0]=-1; tot=0;
    	dfs1(root,0); dfs2(root,root);
    	Tree.build(1,1,n);
    	printf("%d
    ",root-1);
    	flag[root]=1;
    	while (Tree.tree[1].maxn)
    	{
    		int pos=Tree.tree[1].pos;
    		printf("%d
    ",pos-1);
    		for (;!flag[pos];pos=fa[pos])
    		{
    			Tree.update(1,id[pos],id[pos]+size[pos]-1);
    			flag[pos]=1;
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    MySQL统计函数记录——按月、按季度、按日、时间段统计
    Myslq查询字段为null的数据
    表情包
    在线logo制作
    在线图片识别 图片转文字 OCR
    PDF
    php读取access数据库
    java注解的自定义和使用
    zookkeper原理学习
    mybatis源码阅读心得
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998011.html
Copyright © 2020-2023  润新知