题目链接:

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. OOA/OOD/OOP的区别

    http://javajiao.iteye.com/blog/152956这是一个故事: "工程師修了一條隧道,隧道的一端就是美麗的風景,很多人會開車通過隧道.雖然隧道內已經有燈了,但是設計 ...

  2. C语言重要概念汇总

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  3. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  4. 14:质数因子PrimeNum

    14:题目描述 功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 ) 详细描述: 函数接口说明: public String getResult( ...

  5. jdbc 模板 连接

    package itcast; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;i ...

  6. WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where applicable(附编译脚本)

    WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where appli ...

  7. 研究下JavaScript中的Rest參数和參数默认值

    研究下JavaScript中的Rest參数和參数默认值 本文将讨论使 JavaScript 函数更有表现力的两个特性:Rest 參数和參数默认值. Rest 參数 通常,我们须要创建一个可变參数的函数 ...

  8. Javascript文件加载:LABjs和RequireJS

    传统上,加载Javascript文件都是使用<script>标签. 就像下面这样: <script type="text/javascript" src=&quo ...

  9. C# WinForm退出方法

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中 ...

  10. 这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候。

    这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候. package main import ( "bytes" "fmt" ...