codeforces 208 E 时间戳 倍增法求LCA

本文介绍了一种使用倍增法解决森林查询问题的方法。针对给定的森林结构和多个询问,通过预先计算每个节点的祖先信息,快速找出特定节点与其指定祖先相同的所有节点数量。该方法还涉及到了LCA(最近公共祖先)问题的解决思路。

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

链接:http://www.codeforces.com/problemset/problem/208/E

题意:给你一个森林,m个询问:v,p

求有多少个点(除v外) 与 v的第p个祖先相同

这个题首先要解决找某个点的第p个祖先的问题,可以采用倍增法

记录一个二维数组p[u][i]表示u的第2^i个祖先,那么通过这个数组我们就可以知道u的上面任意深度(相对于u)祖先是谁(巧妙的利用二进制)

具体求法和用法见下,还可以找LCA的


/*
2^17=131072;
2^18=262144;
*/
const int POW = 18;
void dfs(int u,int fa){
	d[u]=d[fa]+1;
    p[u][0]=fa;
	for(int i=1;i<POW;i++) p[u][i]=p[p[u][i-1]][i-1];
	int sz=edge[u].size();
	for(int i=0;i<sz;i++){
		int v=edge[u][i];
		if(v==fa) continue;
		dfs(v,u);
	}
}
int lca( int a, int b ){
	if( d[a] > d[b] ) a ^= b, b ^= a, a ^= b;
	if( d[a] < d[b] ){
		int del = d[b] - d[a];
		for( int i = 0; i < POW; i++ ) if(del&(1<<i)) b=p[b][i];
	}
	if( a != b ){
		for( int i = POW-1; i >= 0; i-- ) 
			if( p[a][i] != p[b][i] ) 
			     a = p[a][i] , b = p[b][i];
		a = p[a][0], b = p[b][0];
	}
	return a;
}

这道题的话需要记录  每个点的两个时间戳,每个深度的所有时间戳

假设vp是v的p祖先,那么v 的 p  Cousins就是时间戳位于vp的两个时间戳之间深度为dep【v】

的点的个数

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 100010;
vector<int> g[maxn];
vector<int> edge[maxn];
vector<int> rt;
bool vis[maxn];
int n,m,dfn;
int in[maxn],out[maxn];
int dep[maxn],p[maxn][18];
int depth;
void dfs(int u,int fa){
	vis[u]=true;
	in[u]=dfn++;
//printf("in[%d]=%d\n",u,in[u]);
	dep[u]=dep[fa]+1;
	g[dep[u]].push_back(dfn);
    p[u][0]=fa;
	for(int i=1;i<18;i++) p[u][i]=p[p[u][i-1]][i-1];
	int sz=edge[u].size();
	for(int i=0;i<sz;i++){
		int v=edge[u][i];
		if(v==fa) continue;
		dfs(v,u);
	}
	out[u]=dfn++;
//printf("out[%d]=%d\n",u,out[u]);
}
int solve(int tfn,int d){
	int l=0,r=g[d].size()-1,best=-1;
	while(l<=r){
		int m=l+r>>1;
		if(g[d][m]<=tfn){
			l=m+1;  best=m;
		}
		else r=m-1;
	}
	return best+1;
}
int find(int v,int pp){
	for(int i=0;i<18;i++) if(pp&(1<<i)) v=p[v][i];
	return v;
}
int ans[maxn];
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		int fa,v,pp;
		rt.clear();
		memset(p,0,sizeof(p));
		memset(dep,0,sizeof(dep));
		for(int i=0;i<=n;i++) edge[i].clear(),g[i].clear();
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&fa);
			if(fa==0) rt.push_back(i);
			else edge[fa].push_back(i);
		}
		dfn=0;depth=0;
		for(int i=0;i<rt.size();i++)  depth=0,dfs(rt[i],0);
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&v,&pp);
			int vp=find(v,pp);
			if(!vp) ans[i]=0;
			else {
			    ans[i]=solve(out[vp],dep[v])-solve(in[vp]-1,dep[v])-1;
			}
		}
		for(int i=0;i<m;i++) printf("%d ",ans[i]);
		puts("");

	}
	return 0;
}




### 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?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值