【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
【题意】
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 (最小瓶颈生成树、树上倍增)的更多相关文章
- [NOIP2013/Codevs3287]货车运输-最小[大]生成树-树上倍增
Problem 树上倍增 题目大意 给出一个图,给出若干个点对u,v,求u,v的一条路径,该路径上最小的边权值最大. Solution 看到这个题第一反应是图论.. 然而,任意路径最小的边权值最大,如 ...
- 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)
题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...
- uva 11354 - Bond(树链拆分)
题目链接:uva 11354 - Bond 题目大意:给定一张图.每次询问两个节点路径上进过边的危急值的最大值的最小值. 解题思路:首先建立最小生成数,然后依据这棵树做树链剖分. #include & ...
- 【最小瓶颈生成树】【最小生成树】【kruscal】bzoj1083 [SCOI2005]繁忙的都市
本意是求最小瓶颈生成树,但是我们可以证明:最小生成树也是最小瓶颈生成树(其实我不会).数据范围很小,暴力kruscal即可. #include<cstdio> #include<al ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- 【bzoj2429】[HAOI2006]聪明的猴子(图论--最小瓶颈生成树 模版题)
题意:有M只猴子,他们的最大跳跃距离为Ai.树林中有N棵树露出了水面,给出了它们的坐标.问有多少只猴子能在这个地区露出水面的所有树冠上觅食. 解法:由于要尽量多的猴子能到达所有树冠,便用Kruskal ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
题意: n个城市,m条路,每条路有个危险值,要使得从s走到t的危险值最小.回答q个询问,每个询问有s和t,要求输出从s到t最小的危险值.(5万个点,10万条边) 思路: 其实要求的是任意点对之间的最小 ...
- NOIP2013 货车运输 (最大生成树+树上倍增LCA)
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...
随机推荐
- Java read()和readLine()的区别
1.read() 功能:读取单个字符的个数,如果已经读完的话会返回-1 (其范围从 0 到 65535 ) 例子如下: byte[] buf = new byte[1024]; int len; wh ...
- win 10应用商店下载应用错误码0x80070422
Win10应用商店下载应用提示错误0x80070422怎么办? 一些安装了Win10系统的朋友们在使用过程中发现,在使用WIn8应用商店下载免费应用的时候,系统提示:错误0x80070422,这是怎么 ...
- windows 定时任务
创建定时任务 创建定时任务,时间间隔为1min,开始时间为04:00:00,任务名称为backupSchedule,运行当前目录下的copyData.bat脚本 schtasks /create /s ...
- Windows 7中,用Visual Studio开发WPF应用程序,实现从Windows Explorer中拖拽文件到应用程序,始终显示“无法拖放”符号问题解决方案
Are you running your application or Visual Studio that hosts the app under administrative privilege? ...
- 类型与通用语言运行时:System.Object
CLR 要求每个类型最终都要继承自 System.Object 类型 //隐式继承 Object class Employee { ... } //显示继承继承 Object class Employ ...
- UVA 1401 Remember the Word(用Trie加速动态规划)
Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...
- InkPicture 控件使用_01
private System.ComponentModel.Container components = null; private Microsoft.Ink.InkOverlay m_InkOv ...
- z-index使用以及失效的处理方法
1.一般z-index的使用是在有两个重叠的标签,在一定的情况下控制其中一个在另一个的上方出现. 2.z-index值越大就越是在上层.z-index:9999:z-index元素的position属 ...
- ng表单验证,提交以后才显示错误
只在提交表单后显示错误信息 有时候不想在用户正在输入的时候显示错误信息. 当前错误信息会在用户输入表单时立即显示. 由于Angular很棒的数据绑定特性,这是可以发生的. 因为所有的事务都可以在一瞬间 ...
- 【 java版坦克大战--事件处理】 键盘控制小球上下左右移动
上一节已经学习了事件处理,这一节需要完成通过键盘的上下左右键控制小球移动. 然后再通过应用到我们绘制的坦克上. /** * 加深对事件处理机制的理解 * 通过光标的上下左右键,控制小球的左右上下移动. ...