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大的值是多 ...
随机推荐
- Java正则表达中Greedy Reluctant Possessive 的区别
Java正则表达中Greedy Reluctant Possessive 的区别 分类: java2015-01-16 00:28 1280人阅读 评论(9) 收藏 举报 正则表达式Java 目录 ...
- nodejs 中 excel-export 使用介绍
1. 为了在nodejs 服务器端操作数据导出excel 格式用的 excel-export 包地址:https://github.com/functionscope/Node-Excel-Expo ...
- monkey参数应用
1.指定seed值 adb shell monkey -v -p package -s 100 100 2.touch事件(参数后都跟百分比) 3.设定动作百分比 4.轨迹球 5.基本导航事件 输入 ...
- Gridview中Datakeys 通过主键取得各列的值。
首先在初始化Gridview时候定义主键的数组. GridViewTeacherStudent.DataKeyNames=new string[] {"courseId",&quo ...
- 分享知识-快乐自己:mysql数据库常见两种引擎
mysql的常用引擎 在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 首先: 1.简单介绍这两种引擎,以及该如何去选择. 2.这两种引擎所使用的数据结构是什么. Innod ...
- 编译内核时覆盖KBUILD_BUILD_USER和KBUILD_BUILD_HOST
默认情况下make kernel.img编译出来的内核在/proc/version中显示的内容是: Linux version 3.0.36+ (xxx@yyyy) (gcc version 4.6. ...
- 简单CSS3动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【VS外接程序】利用T4模板生成模块代码
引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...
- 3 Python os 文件和目录
ile 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...
- hdoj-2647-Reward(拓扑排序)
题目链接: /* Name:hdoj-2647-Reward Copyright: Author: Date: 2018/4/11 15:59:18 Description: */ #include ...