LCA的 RMQ解法模版

 

 

 

struct Edge{
	int from, to, nex;
}edge[N<<1];
int head[N], edgenum;
void addedge(int u, int v){
	Edge E = {u, v, head[u]};
	edge[ edgenum ] = E;
	head[u] = edgenum ++;
}

inline int Max(int a,int b){return a>b?a:b;}

int time;
int deep[N<<1], index[N<<1], first[N];
void DFS(int u, int dep){
	deep[time] = u;
	index[time] =u;
	time++;
	for(int i = head[u]; i !=-1; i = edge[i].nex)
	{
		int v = edge[i].to;
		if(first[v] == 0)
		{
			first[v] = time;
			DFS(v, dep+1);
			deep[time] = u;
			index[time]= u;
			time++;
		}
	}
}
int dp[N<<1][25];//注意第二维一定要比log(n)大
void RMQ_init(int n){
	for(int i = 1; i <= n; i++)
		dp[i][0] = i;
	for(int j = 1; (1<<j)<=n;j++)
	{
		int k = 1<<(j-1);
		for(int i = 1; i+k<n; i++)
		{
			if(deep[ dp[i][j-1] ] <= deep[ dp[i+k][j-1] ])
				dp[i][j] = dp[i][j-1];
			else 
				dp[i][j] = dp[i+k][j-1];
		}
	}
}
int RMQ(int a,int b){
	int dis = Max(a-b,b-a) +1;
	int k = log(double(dis))/ log(2.0);
	if(deep[dp[a][k]]<= deep[dp[b - (1<<k) +1][k]])
		return dp[a][k];
	else
		return dp[b-(1<<k)+1][k];
}
int LCA(int u, int v){
	int fu = first[u], fv = first[v];
	return fu<=fv? index[ RMQ(fu,fv)] : index[ RMQ(fv,fu)];
}
void init(){
	memset(head, -1, sizeof(head)); edgenum = 0;
	memset(first, 0, sizeof(first));
}
void Have_Lca(int root){
	first[root] = 1;
	time = 1;
	DFS(root, 0);
	RMQ_init(time-1);
}


posted on 2013-11-14 20:30  you Richer  阅读(202)  评论(0编辑  收藏  举报