【题意】

  n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小

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 ≤ 10^9) – the numbers of the cities connected by the i-th 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.
Sample Input
4 5
1 2 10
1 3 20
1 4 100
2 4 30
3 4 10
2
1 4
4 1
2 1
1 2 100
1
1 2
Sample Output
20
20
100

【分析】

  很明显是最小瓶颈生成树。

  有一个定理:最小生成树是最小瓶颈生成树,但是最小瓶颈生成树不一定是最小生成树。

  我们只要求最小生成树就好了。

  不过这题n较大,不能n^2预处理,所以我们先把树求出来,然后询问的时候 树剖或者倍增 都可以。

  应该是,倍增耗空间但是时间少一个log , 树剖省一点空间但是 时间多一个log (要多一个数据结构维护)

  我上次就打一题倍增MLE了TAT,不过这题正解是倍增,不知道树剖+线段树能不能过。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 50010
#define Maxm 1000010
#define INF 0xfffffff struct node
{
int x,y,c,next;
}t[Maxn*],tt[Maxm];
int len;
int first[Maxn]; bool cmp(node x,node y) {return x.c<y.c;}
int mymax(int x,int y) {return x>y?x:y;} int fa[Maxn];
int ffind(int x)
{
if(fa[x]!=x) fa[x]=ffind(fa[x]);
return fa[x];
} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int f[Maxn][],g[Maxn][],dep[Maxn]; void dfs(int x,int ff,int l)
{
dep[x]=dep[ff]+;
g[x][]=ff;
for(int i=;(<<i)<=dep[x];i++)
g[x][i]=g[g[x][i-]][i-];
f[x][]=l;
for(int i=;(<<i)<=dep[x];i++)
f[x][i]=mymax(f[x][i-],f[g[x][i-]][i-]);
for(int i=first[x];i;i=t[i].next) if(t[i].y!=ff)
dfs(t[i].y,x,t[i].c);
} int ffind(int x,int y)
{
int ans=;
while(dep[x]!=dep[y])
{
int z;
if(dep[x]<dep[y]) z=x,x=y,y=z;
for(int i=;i>=;i--) if(dep[x]-(<<i)>=dep[y])
ans=mymax(ans,f[x][i]),x=g[x][i];
}
if(x==y) return ans;
if(x!=y)
{
for(int i=;i>=;i--) if(g[x][i]!=g[y][i]&&dep[x]>=(<<i))
ans=mymax(ans,f[x][i]),ans=mymax(ans,f[y][i]),
x=g[x][i],y=g[y][i];
}
ans=mymax(ans,f[x][]);ans=mymax(ans,f[y][]);
return ans;
} int main()
{
int n,m;
bool ok=;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(ok) printf("\n");
ok=;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&tt[i].x,&tt[i].y,&tt[i].c);
}
sort(tt+,tt++m,cmp);
int cnt=;
for(int i=;i<=n;i++) fa[i]=i;
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
if(ffind(tt[i].x)!=ffind(tt[i].y))
{
fa[ffind(tt[i].x)]=ffind(tt[i].y);
cnt++;
ins(tt[i].x,tt[i].y,tt[i].c);
ins(tt[i].y,tt[i].x,tt[i].c);
}
if(cnt==n-) break;
}
dep[]=;
dfs(,,);
int q;
scanf("%d",&q);
for(int i=;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",ffind(x,y));
}
}
return ;
}

2016-11-01 08:16:45

【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)的更多相关文章

  1. [NOIP2013/Codevs3287]货车运输-最小[大]生成树-树上倍增

    Problem 树上倍增 题目大意 给出一个图,给出若干个点对u,v,求u,v的一条路径,该路径上最小的边权值最大. Solution 看到这个题第一反应是图论.. 然而,任意路径最小的边权值最大,如 ...

  2. 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)

    题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...

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

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

  4. 【最小瓶颈生成树】【最小生成树】【kruscal】bzoj1083 [SCOI2005]繁忙的都市

    本意是求最小瓶颈生成树,但是我们可以证明:最小生成树也是最小瓶颈生成树(其实我不会).数据范围很小,暴力kruscal即可. #include<cstdio> #include<al ...

  5. POJ 1861 ——Network——————【最小瓶颈生成树】

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15268   Accepted: 5987   Specia ...

  6. 【bzoj2429】[HAOI2006]聪明的猴子(图论--最小瓶颈生成树 模版题)

    题意:有M只猴子,他们的最大跳跃距离为Ai.树林中有N棵树露出了水面,给出了它们的坐标.问有多少只猴子能在这个地区露出水面的所有树冠上觅食. 解法:由于要尽量多的猴子能到达所有树冠,便用Kruskal ...

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

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

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

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

  9. NOIP2013 货车运输 (最大生成树+树上倍增LCA)

    死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...

随机推荐

  1. EasyUI兼容IE问题

    上网搜了下,发现说明白的解决方案不多,于是记录了一下: 根本原因是JQuery的版本造成IE8及以下兼容的问题,(由于EasyUI是基于JQuery框架的,而JQuery貌似从2013年为了简洁代码, ...

  2. c++ 学习之const专题之const成员函数

    一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象: ...

  3. 10个你可能不知道的JavaScript小技巧

    1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做 ...

  4. editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)

    ios8 出来的左滑小菜单 可以自定义想要的按钮 (要求ios8以上) - (nullable NSArray<UITableViewRowAction *> *)tableView:(U ...

  5. iOS 制作发布证书,发布到App Store

    ---恢复内容开始--- 1.登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 2.在 iOS Provisioning Portal中,点击App IDs ...

  6. ARC和MRC实现单例模式

    代码如下,可直接拷贝到头文件中 #define singleton_h(name) +(instancetype)shared##name # if __has_feature(objc_arc) / ...

  7. sqlite3 语句总结

      一. iOS客户端设计数据库时一般使用  sqlite,以sqlite3 为例,简单介绍一下. 二. sqlite3常用命令当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端 ...

  8. DBHelper 数据库帮助类

    /// <summary> /// 数据库帮助类 /// <author>vito</author> /// </summary> public cla ...

  9. Ubuntu系统的安装

    在上一篇博客中,我们已经建立了一个“空白”的虚拟Ubuntu镜像,在这篇博客中,我们将介绍如何安装并进入完整的Ubuntu系统. 写在前面:不同版本的系统在安装过程中,有些操作可能会不同,但是其核心步 ...

  10. 不对称密钥密码体系之RSA

    公钥密码的特性: 1.加密和解密使用不同的钥匙 2.从一个钥匙推出另一个钥匙在计算上不可行 3.每个钥匙都可以做加密和解密 RSA算法: 1978年, MIT三位数学家 R.L.Rivest,A.Sh ...