Bond UVA - 11354(LCA应用题)
Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.
Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xi, yi, di (1 ≤ xi, yi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si and ti (1 ≤ si, ti ≤ N, si !=ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
LCA实际应用的一个题目
题意:
N个城市 M条路 保证可以联通 ,有q个询问,求出从x走到y的最短路径上的最大边长
如果对于LCA还有什么不理解的话,可以看我的上一篇随笔,LCA倍增算法详解。
如果还有不懂可以留言给我
根据题意应该先生成一个最小生成树,然后通过LCA求出最短路径上的最大边长;
此题对于格式卡的非常死。注意格式
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include <iomanip>
using namespace std;
const int maxn=;
const int INF=;
struct node1 {
int u,v,f;
}qu[maxn*];
struct node2 {
int x,y;
node2 (int x= ,int y=):x(x),y(y){};
};
vector<node2>a[maxn];
int n,m,pre[maxn],fa[maxn],rk[maxn],cost[maxn];
int anc[maxn][],maxcost[maxn][];
int cmp1(node1 a,node1 b){
return a.f<b.f;
}
int find(int x){
while(x!=pre[x]) x=pre[x];
return x;
}
void dfs(int u,int f,int depth){
rk[u]=depth;
fa[u]=u;
int len=a[u].size();
for (int i= ;i<len ;i++){
int v=a[u][i].x;
int w=a[u][i].y;
if (v!=f) {
dfs(v,u,depth+);
fa[v]=u;
cost[v]=w;
}
}
}
void lca(){
for (int i= ;i<=n ;i++){
anc[i][]=fa[i];
maxcost[i][]=cost[i];
for (int j= ;(<<j)<=n ;j++){
anc[i][j]=-;
}
}
for (int j= ;(<<j)<=n ;j++){
for (int i= ;i<=n ;i++){
if (anc[i][j-]!=-) {
anc[i][j]=anc[anc[i][j-]][j-];
maxcost[i][j]=max(maxcost[i][j-],maxcost[anc[i][j-]][j-]);
}
}
}
}
int query(int x,int y){
if (rk[x]<rk[y]) swap(x,y);
int k,ans=-INF;
for (k= ;(<<(+k))<=rk[x] ;k++) ;
for (int i=k ;i>= ;i--){
if (rk[x]-(<<i)>=rk[y]) {
ans=max(ans,maxcost[x][i]);
x=anc[x][i];
}
}
if (x==y) return ans;
for (int i=k ;i>= ;i--){
if (anc[x][i]!=- && anc[x][i]!=anc[y][i] ){
ans=max(ans,maxcost[x][i]);
x=anc[x][i];
ans=max(ans,maxcost[y][i]);
y=anc[y][i];
}
}
ans=max(ans,max(cost[x],cost[y]));
return ans;
}
int main() {
int flag= ;
while(scanf("%d%d",&n,&m)!=EOF){
if (flag==) printf("\n");
flag=;
for (int i= ;i<m ;i++)
scanf("%d%d%d",&qu[i].u,&qu[i].v,&qu[i].f);
sort(qu,qu+m,cmp1);
for (int i= ;i<=n ;i++ ){
a[i].clear();
pre[i]=i;
}
for (int i= ;i<m ;i++){
int x=find(qu[i].u);
int y=find(qu[i].v);
if (x!=y) {
pre[x]=y;
a[x].push_back(node2(y,qu[i].f));
a[y].push_back(node2(x,qu[i].f));
}
}
dfs(,-,);
lca();
int q;
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y));
}
}
return ;
}
Bond UVA - 11354(LCA应用题)的更多相关文章
- Uva 11354 LCA 倍增祖先
题目链接:https://vjudge.net/contest/144221#problem/B 题意:找一条从 s 到 t 的路,使得瓶颈路最小. 点的数目是10^4,如果向之前的方案求 maxc ...
- Bond UVA - 11354(并查集按秩合并)
题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- uva 11354 - Bond(树链拆分)
题目链接:uva 11354 - Bond 题目大意:给定一张图.每次询问两个节点路径上进过边的危急值的最大值的最小值. 解题思路:首先建立最小生成数,然后依据这棵树做树链剖分. #include & ...
- UVA 11354 Bond(MST + LCA)
n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...
- UVA 11354 Bond 最小生成树 + lca
题意 给出一张图,q个询问,每次询问给出uv,找出一条路径,使这条路径上的最大边权是两点所有路径中最小,输出这个值 思路 很显然要先求出最小生成树,任意两点在最小生成树上有唯一路径,并且这条路径上的最 ...
- UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
随机推荐
- sqlite不存在记录则插入数据
问题:如下图在Sqlite数据库中存在一张Student表,现需要向表中插入数据,如果不存在同名记录则插入,否则不进行插入操作. 解答:利用not exists语句,如下: insert into [ ...
- Dropout
参数正则化方法 - Dropout 受人类繁衍后代时男女各一半基因进行组合产生下一代的启发,论文(paper.pdf)提出了Dropout. Dropout是一种在深度学习环境中应用的正规化手段.它是 ...
- Photoshop 样式的角度/高度选择器控件
Conmajia © 2012 Updated on Mar. 5, 2018 简介 Adobe Photoshop有两个非常专业的控件:角度选择器和角度与高度选择器,如图1所示. 图1 两种控件外观 ...
- 30分钟掌握ES6/ES2015核心内容(上)
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
- mysql数据库导入导出 查询 修改表记录
mysql数据导入导出: 导入: 把系统的文件的内容,保存到数据库的表里 导入数据的基本格式:mysql> load data infile "文件名" into table ...
- hashtable的运用实例
#include <hash_set> #include <iostream> using namespace std; int main() { hashtable<i ...
- caffe︱ImageData层、DummyData层作为原始数据导入的应用
Part1:caffe的ImageData层 ImageData是一个图像输入层,该层的好处是,直接输入原始图像信息就可以导入分析. 在案例中利用ImageData层进行数据转化,得到了一批数据. 但 ...
- NLP︱高级词向量表达(三)——WordRank(简述)
如果说FastText的词向量在表达句子时候很在行的话,GloVe在多义词方面表现出色,那么wordRank在相似词寻找方面表现地不错. 其是通过Robust Ranking来进行词向量定义. 相关p ...
- 关系网络理论︱细讲中介中心性(Betweeness Centrality)
关系网络在我认为都是一种很简单暴力地能挖掘人群特征关系的一种方式,特别今天去听了一场关于AI与金融领域的结合,里面提到了拓扑分析其实就是关系网络的解释.我在之前的文章( R语言︱SNA-社会关系网络- ...
- arm_linux_device_mem内存映射
/dev/mem: 物理内存的全镜像.可以用来访问物理内存. /dev/kmem: kernel看到的虚拟内存的全镜像.可以用来访问kernel的内容. /dev/mem 用来访问物理IO设备比如X ...