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应用题)的更多相关文章

  1. Uva 11354 LCA 倍增祖先

    题目链接:https://vjudge.net/contest/144221#problem/B 题意:找一条从 s 到 t  的路,使得瓶颈路最小. 点的数目是10^4,如果向之前的方案求 maxc ...

  2. Bond UVA - 11354(并查集按秩合并)

    题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...

  3. 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

    layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...

  4. uva 11354 - Bond(树链拆分)

    题目链接:uva 11354 - Bond 题目大意:给定一张图.每次询问两个节点路径上进过边的危急值的最大值的最小值. 解题思路:首先建立最小生成数,然后依据这棵树做树链剖分. #include & ...

  5. UVA 11354 Bond(MST + LCA)

    n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...

  6. UVA 11354 Bond 最小生成树 + lca

    题意 给出一张图,q个询问,每次询问给出uv,找出一条路径,使这条路径上的最大边权是两点所有路径中最小,输出这个值 思路 很显然要先求出最小生成树,任意两点在最小生成树上有唯一路径,并且这条路径上的最 ...

  7. UVA - 11354 Bond(最小生成树+LCA+瓶颈路)

    题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...

  8. UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)

    题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...

  9. UVA 11354 Bond(最小瓶颈路+倍增)

    题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...

随机推荐

  1. C++学习笔记第一天:基础

    前言 N年前学的C,经过VB.JAVA.JS.C#等后辈的轮番蹂躏,当初学的那点儿东西早就还给老师了 现在有了在桌面端实现 Native + Web 的初衷,需要利用CEF开源组件来封装这个Nativ ...

  2. 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据

     目录 前言 1 Get方式和Post方式接口说明 2 OkHttp库简单介绍及环境配置 3 具体实现 前言 本文具体实现思路和大部分代码参考自<第一行代码>第2版,作者:郭霖:但是文中讲 ...

  3. home目录迁移至新分区

    在用户home目录越来越大时,就可以考虑将home目录迁移至新的分区. 1.创建新分区. fidisk /dev/sda:用磁盘管理器打开磁盘 n:新建 +10g :设置分区为10G w :保存 保存 ...

  4. JavaScript的预编译和执行

    JavaScript引擎,不是逐条解释执行javascript代码,而是按照代码块一段段解释执行.所谓代码块就是使用<script>标签分隔的代码段. 整个代码块共有两个阶段,预编译阶段和 ...

  5. mysql常用基础操作语法(十二)~~常用数值函数【命令行模式】

    数值函数是常用函数之一,也是学习mysql必会的,常用的有如下一些: 1.ceil:返回大于某个数的最小整数值: 2.floor:和上一个相反,返回小于某个数的最大整数值: 3.round:返回某个数 ...

  6. PCI9054 DMA设置流程

    1.设置方式寄存器:设置DMA通道的传输方式,寄存器DMAMODE0或者DMAMODE1的位9:0-表示块传输,1-表示散/聚传输: 2.设置PCI地址寄存器:设置PCI总线侧的地址空间. 3.设置L ...

  7. 笔记+R︱Logistics建模简述(logit值、sigmoid函数)

    本笔记源于CDA-DSC课程,由常国珍老师主讲.该训练营第一期为风控主题,培训内容十分紧凑,非常好,推荐:CDA数据科学家训练营 ---------------------------------- ...

  8. R语言︱基本函数、统计量、常用操作函数

    先言:R语言常用界面操作 帮助:help(nnet) = ?nnet =??nnet 清除命令框中所有显示内容:Ctrl+L 清除R空间中内存变量:rm(list=ls()).gc() 获取或者设置当 ...

  9. 标准的SQL语句类型

    标准的SQL语句类型 1.查询语句:主要是由select关键字完成 2.事务控制语句:主要由commit.rollback和savepoint三个关键字完成 3.DML(数据操作语言)语句:主要由in ...

  10. ffmpeg在am335x上的移植

    交叉编译工具:arm-linux-gcc 一.先下载一下文件 1. yasm-1.2.0.tar.gz 2. x264-snapshot-20140424-2245.tar.bz2 3. xvidco ...