题目链接:

Network

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

Problem Description
 
The 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.
 
Input
 
There are only one test case in input file.
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.
 
Output
 
For 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.
 
Sample Input
 
5 5
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
 
Sample Output
 
3
2
2
invalid request!
 
题意:
 
给一棵树上的每个点的权值和边的连接情况,然后问点l到点r的路径上权值第k大的点,如果k==0,则修改一个点的权值;
 
思路:
 

lca的ST算法;求第k大的时候可以把这条路径上的点的权值都保存下来,排序后输出就行,我还担心会不会tle,最后还是AC了;
 
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)的更多相关文章

  1. HDU 3078 Network(LCA dfs)

    Network [题目链接]Network [题目类型]LCA dfs &题意: 给出n个点的权值,m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上 ...

  2. HDU 3078 Network LCA

    题意:n个点 m个询问,下面一行是n 个点的权值 再下面n-1行是双向的边 然后m个询问:k u v 若k==0,则把u点的权值改为v,否则回答u->v之间最短路经过点的权值中  第k大的值是多 ...

  3. [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 ...

  4. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

  5. hdu 3078 Network (暴力)+【LCA】

    <题目链接> 题目大意:给定一颗带点权的树,进行两种操作,k=0,更改某一点的点权,k!=0,输出a~b路径之间权值第k大的点的点权. 解题分析:先通过RMQ的初始化,预处理pre[]数组 ...

  6. HDU - 3078 Network(暴力+LCA)

    题目大意:给出n个点的权值.m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上第k大的权值点 解题思路:该点的话直接该,找第k大的话直接暴力 #include ...

  7. HDU 3078 Network

    简单的  RMQ:  先预处理得到  所有 节点的 公共祖先  和  dfs 得到所有节点的父亲节点:  然后  询问时,从自己出发向上找父亲, 然后  得到所有的节点:排序一下 不知道  这题这样也 ...

  8. LCA在线算法ST算法

    求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...

  9. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

随机推荐

  1. link标签的rel属性

    <link>标签定义了当前文档与 Web 集合中其他文档的关系.link 元素是一个空元素,它仅包含属性.此元素只能存在于 head 部分,不过它可出现任何次数.在 HTML 中,< ...

  2. CSS解决无空格太长的字母,数字不会自己主动换行的问题

    事实上非常easy,代码例如以下所看到的,注意 Style: <div class="detail_title" style="word-break: break- ...

  3. 安装CoreOS到磁盘

    1 打开翻&&墙软件 2 打开迅雷,启用“使用IE代理”,下载以下两个文件:(翻&&墙后用IE下会中断) http://stable.release.core-os.n ...

  4. ubuntu16.04----jdk---install----config

    1.下载jdk. 2.验证java是否安装,使用java -version命令,如下图所示说明没有安装: 3.在usr目录中创建一个jdk-8目录,如下图所示: 4.配置系统环境变量,编辑/etc/p ...

  5. sealed,new,virtual,abstract与override关键字的区别?

    1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...

  6. MySQL的几种登陆方式

    MySQL的几种登陆方式 登录方式一:    [root@001 tmp]# mysql -h 127.0.0.1 -u root -p 这是最标准的登录方式,意指通过tTCP/IP协议进行连接,因为 ...

  7. 安装部署Solrcloud

    实验说明: 三台虚拟机做solrcloud集群                            安装solr前请确保jdk .tomcat.zookeeper已安装好,否则无法启动 三台虚拟机I ...

  8. 再看GS线程

    再看GS线程 void GameServer::ProcessThreadTry() { ; packet rcvPkt; rcvPkt.data = * ]; //该事件工厂主要创建了两个定时器1. ...

  9. 九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:265 解决:116 题目描述: We start with a stack n of pancakes of distinct sizes. ...

  10. ABAP-创建物料主数据

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' *&------------------------------------------------------- ...