题目链接:

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. C# MVC VS WebAPI

    获取路径: MVC:Server.MapPath("/Templates/vshop/default.json") WebAPI:System.Web.Hosting.Hostin ...

  2. cocos2d-x-lua基础系列教程五(lua单例)

    lua-单例 function newAccount(initlizedBanlance) local self = {balance = initlizedBanlance} local show ...

  3. 高阶函数:sorted

    排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的 ...

  4. RAM和ROM和Flash ROM的区别

    转;http://openedv.com/thread-81182-1-1.html                           http://www.sohu.com/a/112676146 ...

  5. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  6. JavaScript 作用域链图具体解释

    <script type="text/javascript"> /** * 作用域链: */ var a = "a"; function hao94 ...

  7. rtems 4.11 console驱动 (arm, beagle)

    console驱动框架主要文件是 c/src/lib/libbsp/shared/console.c,驱动的入口是 console_initialize()主要作用是初始化BSP提供的全局变量 Con ...

  8. checkStyle使用具体解释

    简单介绍 checkStyle是一款代码格式检查工具.它依据设置好的编码规则来自己主动检查代码.比方命名规范,文件长度.代码行长度等等.代码检查工具是保证项目代码质量.统一编码风格的一种重要途径.本篇 ...

  9. .NET C# Json序列化与反序列化——Newtonsoft.Json学习笔记

    Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(介绍及下载地址:http://json.codeplex.com/). /// <summary>    ...

  10. Linux kernel 2.6下的modules编译与KBuild

    转载:http://blog.sina.com.cn/s/blog_602f87700100dq1u.html Sam之前在Linux kernel 2.4下写过一些driver.但自从转到kerne ...