题目链接:

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. js 解析json字符串

    server端返回的数据例如以下: {"list":[{"id":1,"name":"汉族"},{"id&qu ...

  2. SAS学习经验总结分享:篇一—数据的读取

    第一篇:BASE SAS分为数据步的作用及生成数据集的方式 我是学经济相关专业毕业的,从事数据分析工作近一年,之前一直在用EXCEL,自认为EXCEL掌握的还不错. 今年5月份听说了SAS,便开始学习 ...

  3. 关于C++项目指针对象未被初始化的问题(0xcdcdcd)

    http://blog.csdn.net/devfun/article/details/6900086 昨天我试图将一个封装好的模块加入到正在开发的项目中,这个模块不是单独的类,而且对应的声明和实例. ...

  4. dede内容页调用点击数

     <script src="{dede:field name='phpurl'/}/count.php?view=yes&aid={dede:field name='id'/} ...

  5. Android - 单例模式(singleton)的使用

    单例模式(singleton)的使用 本文地址:http://blog.csdn.net/caroline_wendy 单例(singleton)是特殊的Java类,在创建实例时.一个类仅同意创建一个 ...

  6. SpringMvc aop before

    1.config.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans ...

  7. jvmtop 监控

    1 jar包 <!-- -JVMTOP监控- --> <dependency> <groupId>joda-time</groupId> <art ...

  8. 【EDAS问题】轻量级EDAS部署hsf服务出现找不到类的解决方案

    本地运行轻量级EDAS调用服务的时候报错如下: 2018-01-08 13:16:58.029 WARN [http-bio-8090-exec-8:t.hsf] [RPC Protocol call ...

  9. 语法之知识点的改进(Func/Action)

    上一章我们讲到关于面向对象思想上C#和JAVA之差别.笔者分别从面向对象的三大特性入手.而本章主要讲一些C#改进的知识点.在.NET Framework 2.0之后出现很多新的知识点.这些知识点更是让 ...

  10. php 算法之------------怎样打印出下图

    自己偶尔看到了下图.于是用php打印出下图. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGluZ2ppZ29uZ3Np/font/5a6L5L2T/f ...