Network(lca暴力)
Network
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 5
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.
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.
latency time. Once there are less than k routers in the way, print "invalid
request!" instead.
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
2
2
invalid request!
NUDT
#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暴力)的更多相关文章
- 【bzoj3251】树上三角形 朴素LCA+暴力
题目描述 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. 输入 第一行两个整数n.q表示树的点数和操 ...
- hdu3087 LCA + 暴力
Network Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- HDU 3078 Network(LCA dfs)
Network [题目链接]Network [题目类型]LCA dfs &题意: 给出n个点的权值,m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上 ...
- Network LCA修改点权
Problem Description The ALPC company is now working on his own network system, which is connecting a ...
- HDU 6115 Factory LCA,暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...
- hdu-3078 Network(lca+st算法+dfs)
题目链接: Network Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- POJ——2236Wireless Network(暴力并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 22107 Accepted: 928 ...
- hdu 6115(LCA 暴力)
Factory Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total ...
- HDU 3078 Network LCA
题意:n个点 m个询问,下面一行是n 个点的权值 再下面n-1行是双向的边 然后m个询问:k u v 若k==0,则把u点的权值改为v,否则回答u->v之间最短路经过点的权值中 第k大的值是多 ...
随机推荐
- Eclipse开发快捷键精选
1.alt+?或alt+/:自动补全代码或者提示代码2.ctrl+o:快速outline视图3.ctrl+shift+r:打开资源列表4.ctrl+shift+f:格式化代码5.ctrl+e:快速转换 ...
- Nginx错误日志配置信息详解
Nginx的错误日志可以配置在Main区块,也可以配置在虚拟主机区块中.Nginx软件会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里,是我们调试Nginx服务的重要参考. error ...
- java中如何将非整数保留到小数点后指定的位数
- tomcat添加登录用户名密码
tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...
- Struts2的Action中访问servletAPI方式
struts2的数据存放中心为ActionContext,其是每次请求来时都会创建一个ActionContext,访问结束销毁,其绑定在ThreadLocal上,由于每次访问web容器都会为每次请求创 ...
- ajax02-XMLHttpRequest 对象的使用
XMLHttpRequest 是 AJAX 的基础,用于在后台与服务器交换数据.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. XMLHttpRequest 对象 所有现代浏览器均 ...
- js数组的操作方法
es5中新添加的方法比如map,reduce之类的
- js判断是pc还是移动端
//判断pc还是移动端 var isM = function () { var ua = navigator.userAgent; /* navigator.userAgent 浏览器发送的用户代理标 ...
- Word 2010 怎么在每一章中使用不同的页眉
1.要做到每一章的页眉不同首先要进行 分节 word2010中 页面布局 -> 分隔符 ->下一页 上述操作即可实现分节 2.实现分节后,在每一节开头的那一页,编辑页眉 ...
- jsonp 解决跨域传输
JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅 ...