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 ...
随机推荐
- js中的写出想jquery中的函数一样调用
1.IIFE: Immediately-Invoked function Expression 函数模块自调用 2.代码实现 <!DOCTYPE html> <html lang=& ...
- Java集合框架(六)—— Collections工具类
操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...
- JavaScript实现排序二叉树的相关算法
1.创建排序二叉树的构造函数 /** * 创建排序二叉树的构造函数 * @param valArr 排序二叉树中节点的值 * @constructor */ function BinaryTree(v ...
- Docker系统五:Docker仓库
创建Docker Hub账户 登录和上传镜像到Hub.docker.com docker login //登陆hub.docker.com docker tag ubutun1404-baseimag ...
- AES对称加密
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Secre ...
- Jetty添加Filter过滤器
1.Jetty嵌入到Spring项目 try { Server server = new Server(8080); WebAppContext context = new WebAppContext ...
- (2018干货系列一)最新Java学习路线整合
怎么学Java Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. 话不多说,直接上干货: ...
- Linux CentOS7下安装python3
在CentOS7下,默认安装的就是python2.7,我现在来教大家如何安装python3: 1.首先安装python3.6可能使用的依赖 # yum -y install openssl-devel ...
- MxNet+R︱用R语言实现深度学习(单CPU/API接口,一)
MxNet有了亚马逊站台之后,声势大涨,加之接口多样化,又支持R语言所以一定要学一下.而且作为R语言的fans,为啥咱们R语言就不能上深度学习嘞~ -------------------------- ...
- FusionCharts 2D环饼图
1.静态页面 Doughnut.html: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...