HDU3078(Network)

Network

Problem Description

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個值代表每個點的權。下面n-1行樹邊,操作分爲 0 x y ,表示把點x的權值改爲y; k a b 求出從a到b的路徑中,第k大的點權。最開始覺得這道題不像是LCA的題目,推算的一下暴力複雜度最壞可以達到O(q*nlogn)也就是退化成一條鏈的時候,索性數據好像不太強,暴力跳LCA + 排序也可以過。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
const int maxn = 80005;
const int maxd = 19;
vector<int>e[maxn];
vector<int>s;
int f[maxn][20];
int dep[maxn];
int a[maxn];
inline void clear_set()
{
	memset(f,0,sizeof(f));
	memset(dep,0,sizeof(dep));
	s.clear();
	memset(a,0,sizeof(a));
	for(int i = 0;i < maxn;i++){
		e[i].clear();
	}
}
inline void dfs(int x,int fx)
{
	dep[x] = dep[fx] + 1;
	f[x][0] = fx;
	for(int i = 1;i <= maxd;i++){
		f[x][i] = f[f[x][i-1]][i-1];
	}
	for(int i = 0;i < e[x].size();i++){
		if(e[x][i] == fx)	continue;
		dfs(e[x][i],x);
	}
}
inline int LCA(int x,int y)
{
	if(dep[x] < dep[y])		swap(x,y);
	int d = dep[x] - dep[y];
	for(int i = 0;i <= maxd;i++){			//高度齊平 
		if(((1<<i)&d)){
			x = f[x][i];
		}
	}
	if(x == y)		return x;
	for(int i = maxd;i >= 0;i--){			//同時上跳 
		if(f[x][i] != f[y][i]){
			x = f[x][i];
			y = f[y][i];
		}
	}
	return f[x][0];
} 
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		clear_set();
		for(int i = 1;i <= n;i++){
			scanf("%d",&a[i]);
		}
		int x,y,k;
		for(int i = 1;i < n;i++){
			scanf("%d%d",&x,&y);
			e[x].push_back(y);
			e[y].push_back(x);
		}
		dfs(1,0);
		while(m--){
			scanf("%d%d%d",&k,&x,&y);
			if(k == 0){
				a[x] = y;
				continue;
			}
			int root = LCA(x,y);s.clear();
			//暴力跳LCA加入點權。
			while(x != root){	s.push_back(a[x]);		x = f[x][0];}
			while(y != root){	s.push_back(a[y]);		y = f[y][0];}
			s.push_back(a[root]);
		//	printf("LCA = %d,size = %d\n",root,s.size());
			if(k > s.size()){
				printf("invalid request!\n");
			}
			else{
				sort(s.begin(),s.end(),greater<int>());
				printf("%d\n",s[k-1]);
			}
		}
	}
	return 0;
}

願你走出半生,歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章