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 ...
随机推荐
- bzoj1004: [HNOI2008]Cards(burnside引理+DP)
题目大意:3种颜色,每种染si个,有m个置换,求所有本质不同的染色方案数. 置换群的burnside引理,还有个Pólya过几天再看看... burnside引理:有m个置换k种颜色,所有本质不同的染 ...
- SGU - 282
SGU - 282 题解 题意: 本质不同的集合:不存在两个方案重新编号之后对应的边集相同(对于所有x,y,,(x,y)边颜色都相同). (1≤ N≤ 53, 1≤ M≤ 1000) 对P取模 本质不 ...
- 清华大学计算机系大二 java 小学期考试题(摘自知乎)
public class Main { public void test(Object o) { System.out.println("Object"); } public vo ...
- Friendship POJ - 1815 基本建图
In modern society, each person has his own friends. Since all the people are very busy, they communi ...
- Dynamic len(set(a[L:R])) UVA - 12345(这么过分一定要写博客)
给出一个有n个元素的数组,有以下两种操作:Q x y,求出区间[x,y)内不同元素的个数, M x y,把第x个元素的值修改为y.注意题目中的下标是从0开始的 这题超级超级坑 妈的一个水题找了几个小时 ...
- 自己做的jquery的autocomplete的一个例子
转载自:http://dada-fangfang.iteye.com/blog/695464 首先下载jquery.js和jquery.autocomplete.js 注意:jquery.js 要放在 ...
- 使用 html2canvas 实现浏览器截图
基于上一篇<h5 本地上传图片预览 源码下载>,今天分享一个图片上传后, 根据所上传的图片颜值随机生成一个答案, 并且可以生成一张专属于自己的名片. 首先上传预览我们已经实现了, 所以接下 ...
- 「模板」 线段树——区间乘 && 区间加 && 区间求和
「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...
- pythonweb框架
https://www.cnblogs.com/sss4/p/8097653.html
- 【TYVJ】P1038 忠诚
[算法]线段树 #include<cstdio> #include<algorithm> using namespace std; ]; ,inf=0x3f3f3f3f; in ...