Network

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 5
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!
 
Source
2009 Multi-University Training Contest 17 - Host by
NUDT
 
找lca路中第k大的数,0开头代表改权值
#include <iostream>
#include<cstring>
#include <string>
#include <algorithm>
using namespace std;
int f[];
int vis[];
int head[];
int dep[];
int qv[];
int cnt=;
int a[];
struct node
{
int v;
int nxt;
}e[*]; bool cmp(int x,int y)
{
return x>y;
} void add(int u,int v)
{
e[++cnt].nxt=head[u];
e[cnt].v = v;
head[u]=cnt;
return;
} void dfs(int u,int ff)
{
f[u]=ff;
dep[u]=dep[ff]+;
for(int i=head[u];i!=-;i=e[i].nxt)
{
if(e[i].v!=ff)//要防止重复搜索,tla了多次
{
dfs(e[i].v,u);
}
}
} int kk;
void lca(int x,int y)
{
//逻辑关系要理清
kk=;
if(dep[x]<dep[y])
{
swap(x,y);
}
while(dep[x]>dep[y])
{
a[kk++]=qv[x];
x=f[x];
}
if(x==y)
{
a[kk++]=x;
return;
}
if(f[x]==f[y])
{
a[kk++]=qv[x];
a[kk++]=qv[y];
a[kk++]=qv[f[x]];
return;
}
while(f[x]!=f[y])//直接暴力
{
a[kk++]=qv[x];
a[kk++]=qv[y];
x=f[x];
y=f[y];
}
a[kk++]=qv[x];
a[kk++]=qv[y];
a[kk++]=qv[f[x]];
return;
} int main()
{
int n,m;
scanf("%d %d",&n,&m);
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
{
scanf("%d",&qv[i]);
}
for(int i=;i<=n-;i++)
{
int u,v;
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
f[]=-;
dep[]=;
dfs(,);
for(int i=;i<=m;i++)
{
int u,v,p;
scanf("%d %d %d",&p,&u,&v);
if(p!=)
{ lca(u,v);
if(p>kk-) cout<<"invalid request!"<<endl;
else
{
sort(a+,a+kk,cmp);//要从大到小排
cout<<a[p]<<endl;
}
}
else qv[u]=v;
}
return ; }
 

Network(lca暴力)的更多相关文章

  1. 【bzoj3251】树上三角形 朴素LCA+暴力

    题目描述 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. 输入 第一行两个整数n.q表示树的点数和操 ...

  2. hdu3087 LCA + 暴力

    Network Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  3. HDU 3078 Network(LCA dfs)

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

  4. Network LCA修改点权

    Problem Description The ALPC company is now working on his own network system, which is connecting a ...

  5. HDU 6115 Factory LCA,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...

  6. hdu-3078 Network(lca+st算法+dfs)

    题目链接: Network Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) P ...

  7. POJ——2236Wireless Network(暴力并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 22107   Accepted: 928 ...

  8. hdu 6115(LCA 暴力)

    Factory Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total ...

  9. HDU 3078 Network LCA

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

随机推荐

  1. Go 功能测试与性能测试

    1.功能测试 calcTriangle.go // 需要被测试的函数 func calcTriangle(a, b int) int { return int(math.Sqrt(float64(a* ...

  2. Kubernetes Kubeadm部署集群

    Kubernetes高可用架构 Kubenetes 2个高可用核心 apiserver.etcd etcd:集群数据中心,需要保持高可用,用于存放集群的配置信息.状态信息及Pod等信息.如果数据丢失集 ...

  3. 去除带有iframe页面中的2个滚动条[转]

    方法一:加载frame时修改高度 <div>    <iframe id="frame_content" name="frame_content&quo ...

  4. Maven配置一键部署远程Tomcat

    1. 首先需要配置远程Tomcat的访问权限(设置访问用户名密码) http://www.cnblogs.com/liuchao102/p/5519345.html 2. 配置pom.xml 添加to ...

  5. ADO.Net连接Oracle

    1.添加 Oracle.ManagedDataAccess.dll 2.连接Oracle的实例得添加到Oracle的监听器中,不然会报“ORA-12514: TNS: 监听程序当前无法识别连接描述符中 ...

  6. EasyUI学习

    1.基础知识: 1)Parser解析器: div指定了class后能有效果是因为开始时文档时加载DOM但是一些由js动态生成的指定了class的div没有被解析此时就需要手动解析了 js动态生成的指定 ...

  7. 树分治learning

    学习了树的点分治,树的边分治似乎因为复杂度过高而并不出众,于是没学 自己总结了一下 有些时候面对一些树上的结构 并且解决的是和路径有关的问题的时候 如果是多个询问 关注点在每次给出两个点,求一些关于这 ...

  8. Spring学习之xml配置Bean总结

    学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml: pom.xml <project xmlns="http://maven.apache.org ...

  9. linux-RabbitMQ安装命令

    一.RabbitMQ 1.安装配置epel源    $ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.no ...

  10. tensorflow实现Minist手写体识别

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #下载MINIST数据集mnist ...