codeforces 1062E

本文介绍了一种在树形数据结构中进行LCA(最近公共祖先)查询的优化算法,通过维护区间DFS序最大和次大、最小和次小值,实现了对指定区间内删除一个点后剩余点LCA深度的最大值计算。该算法适用于解决类似Codeforces竞赛题目E的问题,提供了一种高效的数据结构解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:https://codeforces.com/contest/1062/problem/E

题意:给你一棵树,每次询问[l,r]区间删掉一个点后,剩余的点的lca深度最大是多少,输出删除的点和lca深度。

题解:首先一堆点的lca就是dfs序最大的点和最小的点的lca,那么我们只用考虑删掉这两个点后的lca是多少就可以了,维护区间dfs序最大和次大,最小和次小。

#include<bits/stdc++.h>
using namespace std;
int head[100005],cnt,bi[100005],num;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
typedef pair<int,int> pi;
pi p[4];
#define fi first
#define se second
#define mp make_pair
struct nodes{
	pi zd,cd,zx,cx;
}tree[400005];
struct node{
	int to,nex;
}edge[200005];
void build(int t,int l,int r){
	if(l==r){
		tree[t].zd=tree[t].zx=mp(bi[l],l);
		tree[t].cx=mp(10000000,0);
		return ;
	}
	int mid=l+r>>1;
	build(t<<1,l,mid);
	build(t<<1|1,mid+1,r);
	p[0]=tree[t<<1].zd;
	p[1]=tree[t<<1].cd;
	p[2]=tree[t<<1|1].zd;
	p[3]=tree[t<<1|1].cd;
	sort(p,p+4);
	tree[t].zd=p[3];
	tree[t].cd=p[2];
	p[0]=tree[t<<1].zx;
	p[1]=tree[t<<1].cx;
	p[2]=tree[t<<1|1].zx;
	p[3]=tree[t<<1|1].cx;
	sort(p,p+4);
	tree[t].zx=p[0];
	tree[t].cx=p[1];
}
void add(int u,int v){
	edge[cnt].to=v;
	edge[cnt].nex=head[u];
	head[u]=cnt++;
}
pi nzd,ncd,nzx,ncx;
void query(int t,int l,int r,int x,int y){
	if(x<=l&&y>=r){
		p[0]=tree[t].zd;
		p[1]=tree[t].cd;
		p[2]=nzd;
		p[3]=ncd;
		sort(p,p+4);
		nzd=p[3];
		ncd=p[2];
		p[0]=tree[t].zx;
		p[1]=tree[t].cx;
		p[2]=nzx;
		p[3]=ncx;
		sort(p,p+4);
		nzx=p[0];
		ncx=p[1];
		return ;
	}
	int mid=l+r>>1;
	if(y<=mid)query(t<<1,l,mid,x,y);
	else if(x>mid)query(t<<1|1,mid+1,r,x,y);
	else{
		query(t<<1,l,mid,x,y);
		query(t<<1|1,mid+1,r,x,y);
	}
}
int f[100005][20],dep[100005];
int lca(int x,int y){
	if(x==0||y==0)return 0;
    if(dep[x]<dep[y])swap(x,y);
//令x为深度较深的点
    drep(i,16,0)if(dep[f[x][i]]>=dep[y])x=f[x][i];
//让x向上走到与y同一深度
    if(x==y)return x; //如果直接是lca,直接返回
    drep(i,16,0)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
//x,y同时向上走,直到父节点相同
    return f[x][0]; //返回父节点
}
void dfs(int t,int fa,int d){
	f[t][0]=fa;
	bi[t]=++num;
	dep[t]=d;
	for(int i=head[t];~i;i=edge[i].nex){
		int v=edge[i].to;
		if(v==fa)continue;
		dfs(v,t,d+1);
	}
}
#define fuck(x) cout<<#x<<' '<<x<<endl
int main(){
	int n,q;
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&q);
	for(int i=2;i<=n;i++){
		int x;
		scanf("%d",&x);
		add(x,i);
		add(i,x);
	}
	dfs(1,1,1);
	build(1,1,n);
	rep(k,1,16) //假设10W个点
	rep(i,1,n)f[i][k]=f[f[i][k-1]][k-1]; 
	while(q--){
		int l,r;
		scanf("%d%d",&l,&r);
		nzd=ncd=mp(0,0);
		nzx=ncx=mp(n+1,n+1);
		query(1,1,n,l,r);
		int anslab=nzx.se,ansdep=dep[lca(nzd.se,ncx.se)];
//		fuck(ansdep);
//		fuck(dep[lca(ncd.se,nzx.se)]);
		if(dep[lca(ncd.se,nzx.se)]>ansdep){
			ansdep=dep[lca(ncd.se,nzx.se)];
			anslab=nzd.se;
		}
		printf("%d %d\n",anslab,ansdep-1);
	}
	return 0;
}
/*
11 5
1 1 3 3 3 4 2 7 7 6
4 8
1 11
9 11
8 11

*/

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值