hdu-3078 Network(lca+st算法+dfs)
题目链接:
Network
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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.
lca的ST算法;求第k大的时候可以把这条路径上的点的权值都保存下来,排序后输出就行,我还担心会不会tle,最后还是AC了;
#include <bits/stdc++.h>
using namespace std;
const int N=8e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,q,head[N],cnt=,num=,dp[*N][],first[N],fa[N],vis[N],a[*N],dep[N],rec[N],va[N];
struct Edge
{
int to,next;
};
Edge edge[*N];
int cmp(int x,int y)
{
return x>y;
}
void addedge(int s,int e)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
head[s]=cnt++;
}
int dfs(int x,int deep)
{
vis[x]=;
a[num]=x;
dep[x]=deep;
first[x]=num++;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
fa[y]=x;
dfs(y,deep+);
a[num++]=x;
}
}
}
void RMQ()
{
for(int i=;i<num;i++)//dp[i][j]表示区间[i,i+2^j-1]内的最小值;ST算法是基于dp的算法;
{
dp[i][]=a[i];
}
for(int j=;(<<j)<=num;j++)
{
for(int i=;i+(<<j)-<num;i++)
{
if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])
{
dp[i][j]=dp[i][j-];
}
else dp[i][j]=dp[i+(<<(j-))][j-];
}
}
}
int query(int x,int y)
{
int temp=(int)(log((y-x+)*1.0)/log(2.0));
if(dep[dp[x][temp]]<dep[dp[y-(<<temp)+][temp]])return dp[x][temp];
else return dp[y-(<<temp)+][temp];//区间[x,x+2^temp-1]+[y-2*temp+1,y]肯定包含区间[x,y];所以结果是正确的;
}
int get_ans(int x,int y,int d)
{
int lca;
if(first[x]<first[y])
lca=query(first[x],first[y]);
else lca=query(first[y],first[x]);
int counter=;
rec[counter++]=va[lca];
while(x!=lca)
{
rec[counter++]=va[x];
x=fa[x];
}
while(y!=lca)
{
rec[counter++]=va[y];
y=fa[y];
}
if(counter<d)printf("invalid request!\n");
else
{
sort(rec,rec+counter,cmp);
printf("%d\n",rec[d-]);
}
}
int main()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&va[i]);
}
int l,r;
for(int i=;i<n;i++)
{
scanf("%d%d",&l,&r);
addedge(l,r);
addedge(r,l);
}
fa[]=;
dfs(,);
RMQ();
int k,u,v;
while(q--)
{
scanf("%d%d%d",&k,&u,&v);
if(k==)va[u]=v;
else
{
get_ans(u,v,k);
}
}
return ;
}
hdu-3078 Network(lca+st算法+dfs)的更多相关文章
- HDU 3078 Network(LCA dfs)
Network [题目链接]Network [题目类型]LCA dfs &题意: 给出n个点的权值,m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上 ...
- HDU 3078 Network LCA
题意:n个点 m个询问,下面一行是n 个点的权值 再下面n-1行是双向的边 然后m个询问:k u v 若k==0,则把u点的权值改为v,否则回答u->v之间最短路经过点的权值中 第k大的值是多 ...
- [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]
参考: 1. 郭华阳 - 算法合集之<RMQ与LCA问题>. 讲得很清楚! 2. http://www.cnblogs.com/lazycal/archive/2012/08/11/263 ...
- HDU 5289 Assignment (ST算法区间最值+二分)
题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...
- hdu 3078 Network (暴力)+【LCA】
<题目链接> 题目大意:给定一颗带点权的树,进行两种操作,k=0,更改某一点的点权,k!=0,输出a~b路径之间权值第k大的点的点权. 解题分析:先通过RMQ的初始化,预处理pre[]数组 ...
- HDU - 3078 Network(暴力+LCA)
题目大意:给出n个点的权值.m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上第k大的权值点 解题思路:该点的话直接该,找第k大的话直接暴力 #include ...
- HDU 3078 Network
简单的 RMQ: 先预处理得到 所有 节点的 公共祖先 和 dfs 得到所有节点的父亲节点: 然后 询问时,从自己出发向上找父亲, 然后 得到所有的节点:排序一下 不知道 这题这样也 ...
- LCA在线算法ST算法
求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
随机推荐
- iOS 依据文本内容为TextView动态定义高度
解决方式: 1.定义一个textview,在storyboard中设定该textview的constraints. 2.将高度的constraint定义到头文件里:(直接拖拽) @property ( ...
- MongoDB 快速入门
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. MongoDB No ...
- swiper-demo1
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AspectJ学习笔记2-Eclipse中AspectJ插件AJDT的正确安装方法
接着之前一篇日志. 这个事情也挺无语的.简单记录一下. 在这里:http://www.eclipse.org/ajdt/ 能够下载最新的Eclipse Plugin.下载解压之后,一般来说.直接把解压 ...
- Arrays.asList引起的java.lang.UnsupportedOperationException解决方法
在项目中对List进行操作时报错java.lang.UnsupportedOperationException,后来发现操作的List是由数组转换而成的,通过看源码发现问题,并写测试程序如下. 代码块 ...
- Java设计模式(九)责任链模式 命令模式
(十七)责任链模式 责任链模式的目的是通过给予多个对象处理请求的机会,已解除请求发送者与接受者之间的耦合关系.面对对象的开发力求对象之前保持松散耦合,确保对象各自的责任最小化.这种设计能够使得系统更加 ...
- Xenomai 3 POSIX
Xenomai 3在架构设计上确实优先Xenomai 2,至少对开发者来说,少维护了不少东西,看下面两张图就知道了 第一张图是Xenomai2的,第二张图是Xenomai3的,Xenomai3在内核中 ...
- session 购物车
package session; import java.io.IOException;import java.util.ArrayList;import java.util.List; import ...
- Makefile浅尝
[0]README makefile定义: 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要一先编译,哪些文件需要后编译,哪 ...
- POJ 1163 The Triangle(经典问题教你彻底理解动归思想)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38195 Accepted: 22946 De ...