Network HDU - 3078 LCA倍增

The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect all routers. 
The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end. 
Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help. 

Input

There are only one test case in input file. 
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers. 
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000. 
Then n integers in second line refer to the latency of each router in the very beginning. 
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y. 
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000. 
A blank line follows after each case. 

Output

For each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.

Sample Input

5 5
5 1 2 3 4
3 1
2 1
4 3
5 3
2 4 5
0 1 2
2 2 3
2 1 4
3 3 5

Sample Output

3
2
2
invalid request!

题意:n个点q个询问,每个询问给出k,x,y,如果k为0,那么就把点x的权值改为y,否则输出x到y的最短路径中第k大的点的权值。

思路:求出x,y的lca,求出x到lca的最短路和y到lca的最短路,然后可知x到y的最短路,然后将路径上的点从大到小排个序,输出第k个即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=80010;
struct node{
	int id;
	int next;
}side[maxn*2];
int head[maxn],f[maxn][30],deep[maxn],val[maxn],ans[maxn],cnt,n,q,cont;
bool cmp(int a,int b)
{
	return a>b;
}
void init()
{
	memset(head,-1,sizeof(head));
	memset(f,0,sizeof(f));
	cnt=deep[1]=0;
}
void add(int x,int y)
{
	side[cnt].id=y;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
void dfs(int x,int fa)
{
	for(int i=head[x];i!=-1;i=side[i].next)
	{
		int y=side[i].id;
		if(y==fa) continue;
		deep[y]=deep[x]+1;
		f[y][0]=x;
		dfs(y,x);
	}
} 
void lca_init()
{
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i<=n;i++)
			f[i][j]=f[f[i][j-1]][j-1];
}
int lca(int a,int b)
{
	if(deep[a]>deep[b])
		swap(a,b);
	int ff=deep[b]-deep[a];
	for(int i=0;(1<<i)<=ff;i++)
		if((1<<i)&ff)
			b=f[b][i];
	if(a!=b)
	{
		for(int i=int(log2(n));i>=0;i--)
			if(f[a][i]!=f[b][i])
			{
				a=f[a][i];
				b=f[b][i];
			}
		a=f[a][0];
	}
	return a;
}
void solve(int s,int e)
{
	while(s!=e)
	{
		ans[cont++]=val[s];
		s=f[s][0];
	}
	ans[cont++]=val[e];
}
int main()
{
	init();
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",&val[i]);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs(1,-1);
	lca_init();
	while(q--)
	{
		int k,x,y;
		scanf("%d%d%d",&k,&x,&y);
		if(k==0)
		{
			val[x]=y;
		}
		else
		{
			cont=0;
			int fa=lca(x,y);
			solve(x,fa);
			solve(y,fa);
			cont--;
			if(k>cont)
			{
				printf("invalid request!\n");
				continue;
			}
			else
			{
				sort(ans,ans+cont,cmp);
				printf("%d\n",ans[k-1]);
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88758059