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大的值是多 ...
随机推荐
- Logistic回归python实现
2017-08-12 Logistic 回归,作为分类器: 分别用了梯度上升,牛顿法来最优化损失函数: # -*- coding: utf-8 -*- ''' function: 实现Logistic ...
- tomcat添加登录用户名密码
tomcat版本 apache-tomcat-7.0.55.tar.gz 编辑 TOMCAT_HOME/conf/tomcat-users.xml在tomcat-users里面添加 <tomca ...
- C# 处理base64 以及base64的原理分析
base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxiaohui/ar ...
- spark HelloWorld程序(scala版)
使用本地模式,不需要安装spark,引入相关JAR包即可: <dependency> <groupId>org.apache.spark</groupId> < ...
- Java -- DBUtils 框架 操作MySQL数据库
1. 增删改查 常用Handler处理器示例 QueryRunner类提供了两个构造方法: •默认的构造方法 •需要一个 javax.sql.DataSource来作参数的构造方法. public ...
- QT 使用QTcpServer QTcpSocket 建立TCP服务器端 和 客户端
1. 如图客户端连接server后,server发送"hello tcp" 给客户端 2. 实例代码 ----------------------------------- s ...
- Mac下git安装(使用Xcode)
(不使用Xcode的出门右转百度其他方法:比如直接安装git软件包.) 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode ...
- 字典:dict.c/dict.h
Redis 源码分析(1):字典和哈希表(dict.c 和 dict.h)http://huangz.iteye.com/blog/1455808两个点:字典结构的运作流程哈希表的渐进式 rehash ...
- 分享知识-快乐自己:HttpClient 访问 WebService 开放接口
HttpClient: 场景需求如下: 1.项目中需要与一个基于HTTP协议的第三方的接口进行对接 2.项目中需要动态的调用WebService服务(不生成本地源码) 3.项目中需要利用其它网站的相关 ...
- 一张图入门QT设计器
哈哈哈,这个名字听起来就是骗阅读量的.