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 ; }
 

最新文章

  1. Python自动化之YAML解析
  2. 启动mysql出现了error the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)
  3. Jedis编程设计:连接池
  4. jquery 延迟加载代码
  5. Chapter 1 Securing Your Server and Network(13):配置端点安全性
  6. selenium webdriver 如何实现将浏览器滚动条移动到某个位置
  7. tensorflow用dropout解决over fitting-【老鱼学tensorflow】
  8. javascript高级程序设计第3版——第1Java章 DOM扩展
  9. 【转】干货 | 【虚拟货币钱包】从 BIP32、BIP39、BIP44 到 Ethereum HD Wallet
  10. 剑指Offer_编程题_2
  11. sublime text3 golang插件(golang build)
  12. 使用svn导入项目
  13. nginx自旋锁
  14. 修复 Tween.JS 的 onStop 设置无效
  15. 用 iOS-System-Services 框架获取iOS设备所用的设备信息
  16. CSAPP 读书笔记 - 2.31练习题
  17. 运行第一个ruby程序
  18. Android碎笔录3——点击跳转
  19. python算法之近似熵、互近似熵算法
  20. iframe中,页面转换后回到页面的顶部

热门文章

  1. 吴恩达深度学习笔记(九) —— FaceNet
  2. IntelliJ IDEA 中 右键新建时,选项没有Java class的解决方法和具体解释
  3. MapReduce-多个输出(使用MultipleOutput,不指定reduce任务个数)
  4. HDU 5925 离散化
  5. Codeforces Round #370 (Div. 2) A , B , C 水,水,贪心
  6. Python ssh 远程执行shell命令
  7. NovaException: Unexpected vif_type=binding_failed
  8. .parent()和.parents()的区别
  9. Sublime Text3取消自动补全结束标签
  10. CodeForces 444C 线段树