hdu 3078(LCA的在线算法)
Network
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 847    Accepted Submission(s): 347
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.
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.
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.
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
2
2
invalid request!
/*
5 5
5 1 2 3 4 3 1 2 1 4 3 5 3
2 4 5
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
#define N 80005
using namespace std; struct Edge{
int u,v,next;
}edge[*N];
int head[N];
int deep[*N];
int vis[N];
int first[N];
int ver[*N];
int father[N];
int dp[*N][];
int value[N];
int path[N];
int tot; void add_edge(int u,int v,int &k){ ///Á´Ê½Ç°ÏòÐÇ
edge[k].u = u,edge[k].v = v;
edge[k].next = head[u];head[u] = k++;
}
void dfs(int u,int dep,int pre){
vis[u]=true,ver[++tot]=u,first[u]=tot,deep[tot]=dep,father[u]=pre;
for(int k=head[u];k!=-;k=edge[k].next){
if(!vis[edge[k].v]){
dfs(edge[k].v,dep+,u);
ver[++tot] = u,deep[tot]=dep;
}
}
}
int MIN(int i,int j){
if(deep[i]<deep[j]) return i;
return j;
}
void init_RMQ(int n){
for(int i=;i<=n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++){
for(int i=;i+(<<j)-<=n;i++){
dp[i][j] = MIN(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int RMQ(int L,int R){
int k = (int)(log(R-L+1.0)/log(2.0));
int a = dp[L][k];
int b = dp[R-(<<k)+][k];
return MIN(a,b);
}
int LCA(int a,int b){
int x = first[a],y = first[b];
//printf("%d %d\n",deep[x],deep[y]);
if(x>y) swap(x,y);
//printf("RMQ(x,y): %d\n",RMQ(x,y));
return ver[RMQ(x,y)];
}
int cmp(int a,int b){
return a>b;
}
void solve(int k,int u,int v){
int lca = LCA(u,v);
tot=;
while(u!=lca){
path[tot++] = value[u];
u = father[u];
}
while(v!=lca){
path[tot++] = value[v];
v = father[v];
}
//printf("lca = %d\n",lca);
path[tot++] = value[lca];
//printf("path[tot-1] = %d\n",path[tot-1]);
if(k>tot){
printf("invalid request!\n");
return;
}
sort(path,path+tot,cmp);
//for(int i=0;i<tot;i++) printf("%d ",path[i]);
printf("%d\n",path[k-]);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-,sizeof(head));
memset(father,-,sizeof(father));
for(int i=;i<=n;i++){
scanf("%d",&value[i]);
}
tot = ;
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v,tot);
add_edge(v,u,tot);
}
tot = ;
dfs(,,-);
init_RMQ(*n-);
while(m--){
int k,a,b;
scanf("%d%d%d",&k,&a,&b);
if(k==){
value[a]=b;
}else{
solve(k,a,b);
}
}
}
return ;
}
hdu 3078(LCA的在线算法)的更多相关文章
- LCA(倍增在线算法)    codevs 2370 小机房的树
		
codevs 2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点, ...
 - HDU 3078 (LCA+树链第K大)
		
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个 ...
 - HDU 3078 LCA转RMQ
		
题意: n个点 m个询问 下面n个数字表示点权值 n-1行给定一棵树 m个询问 k u v k为0时把u点权值改为v 或者问 u-v的路径上 第k大的数 思路: LCA转RMQ求出 LCA(u,v) ...
 - LCA(最近公共祖先)——dfs+ST 在线算法
		
一.前人种树 博客:浅谈LCA的在线算法ST表 二.沙场练兵 题目:POJ 1330 Nearest Common Ancestors 题解博客:http://www.cnblogs.com/Miss ...
 - LCA最近公共祖先 ST+RMQ在线算法
		
对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决. 这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...
 - LCA在线算法ST算法
		
求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...
 - LCA在线算法详解
		
LCA(最近公共祖先)的求法有多种,这里先介绍第一种:在线算法. 声明一下:下面的内容参考了http://www.cnblogs.com/scau20110726/archive/2013/05/26 ...
 - POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
		
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
 - POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
		
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
 
随机推荐
- oracle-java7-installer安装java失败之后的处理
			
最开始尝试使用installer安装jdk7,但是未能进行完整,之后每次安装软件都会报错,说oracle-java7-installer处有错误,查得如下解决办法: sudo rm /var/lib/ ...
 - Python Pandas与Numpy中axis参数的二义性
			
Stackoverflow.com是程序员的好去处,本公众号将以pandas为主题,开始一个系列,争取做到每周一篇,翻译并帮助pandas学习者一起理解一些有代表性的案例.今天的主题就是Pandas与 ...
 - HDU 5656
			
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
 - CCPC-Winter Camp div2 day1
			
A:机器人 传送门:https://www.zhixincode.com/contest/1/problem/A 题意:地图是由A.B两根线组成的,机器人一开始是在A线上的S点,他初始时可以选择任意方 ...
 - 在IIS中寄存服务
			
http://blog.csdn.net/songyefei/article/details/7381595 第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型 ...
 - RabbitMQ的使用总结
			
RabbitMQ介绍 说明: Consumer (消费者):使用队列 Queue 从 Exchange 中获取消息的应用. Exchange (交换机):负责接收生产者的消息并把它转到到合适的队列. ...
 - uva 10683 Fill
			
https://vjudge.net/problem/UVA-10603 题意: 倒水问题,输出最少的倒水量和目标水量 如果无解,目标水量就是尽可能接近给定点的目标水量,但不得大于给定的目标水量 推推 ...
 - jenkins slave agent 当作服务运行
			
1. 接上边编辑好文件 2. 双击以上的jnlp文件 3. 点击弹出的窗口File->save as service, 此时如果报错的话很可能是由于没有安装.net(.net2 以上) 4. 保 ...
 - 【BZOJ1272】Gate Of Babylon [Lucas][组合数][逆元]
			
Gate Of Babylon Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description Input ...
 - 【BZOJ4069】【APIO2015】巴厘岛的雕塑 [贪心][DP]
			
巴厘岛的雕塑 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 印尼巴厘岛的公路上有许多的雕塑, ...