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 ...
随机推荐
- JavaScript正则表达式的浏览器的差异
JavaScript中的正则表达式在不同的浏览器中得到的结果可能会有差异,下面把正则表达式在五大主流浏览器(IE.Firefox.Chrome.Safari.Opera,以当前版本为准)之间的差异整理 ...
- 学习opencv-------函数使用一
#include"head.h" //cvResize() /*int main() { IplImage *img = cvLoadImage("e:/picture/ ...
- 000 Python常识与快捷键(未完)
1.Python控制台IDLE的快捷键 Alt + N :返回开始输入的第一条语句 Alt + P :返回刚刚输入的上一条语句 Tab:制表符,用于缩进或补全内容,是Python语法格式的灵魂,作用涵 ...
- Leetcode 200. 岛屿的个数(扩展)
1.题目描述 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- MyBatis框架的使用及源码分析(十) CacheExecutor,SimpleExecutor,BatchExecutor ,ReuseExecutor
Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...
- UnknownHostException
1.查看Centos版本号,不同版本修改的方式可能不一样 cat /etc/issue 查看版本 2.通过hostname命令查看当前主机名 hostname 3.编辑network文件修改hostn ...
- bzoj1862/1056: [Zjoi2006]GameZ游戏排名系统
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.ph ...
- JS之document例题讲解2
例题三.图片轮播 <body> <div style="width:1000px; height:250px; margin-top:30px"> < ...
- 在前端发起ajax遇到问题
1.请注意设置datatype的类型. 如下图:
- HashMap根据value获取key值
public static String getCityId(HashMap<String,String> citys, String city){ Set set = citys.ent ...