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 ...
随机推荐
- Golang命名规范和开发规范
目录 命名 文件命名 package 变量 常量 接口 结构体 方法 注释 README 命名 文件命名 文件命名一律采用小写,不用驼峰式,尽量见名思义,看见文件名就可以知道这个文件下的大概内容. 其 ...
- 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...
- sgu 131 状压DP
棋盘覆盖(二) 时间限制:1000 ms | 内存限制:65535 KB 描述 The banquet hall of Computer Scientists' Palace has a ...
- maven中net.sf.json报错的解决方法(转载)
原文:http://www.cnblogs.com/winner-0715/p/5928514.html 今天在用maven添加net.sf.json的jar包的时候,代码如下: <depend ...
- 牛客多校第五场-D-inv
链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...
- tcp/ip 学习-通过视频学习
视频下载地址:http://down.51cto.com/zt/5518/ http://www.icoolxue.com/album/show/328 每天可以拿两个番茄钟看视频,主要目的还是了解, ...
- Enterprise Architect 13 : 设置默认代码环境
设置默认代码环境: Configure -> Options -> Source Code Engineering -> Default Language For Code Gene ...
- centos6.8+openvpn实现账户密码连接(通过端口映射的方式)
#搭建openvpn(编译安装) 初始化环境 #update epel mirror yum install wget -y cd /etc/yum.repos.d && rm -rf ...
- Xamarin入门浅析
1. 安装 1) 使用标准安装流程(JDK1.6 -> Android SDK -> NDK -> Xamarin Studio -> Xamarin Visual Studi ...
- 【比赛】洛谷夏令营NOIP模拟赛
Day1 第一题 水题 第二题 题意:一个n*m的字符矩阵从左上到右下,经过字符形成回文串的路径数.n≤500 回文串,考虑两段往中间DP. f[k][x][y]表示走了k步,左上点横坐标为x,右下点 ...