UVA 11354 Bond(MST + LCA)
n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路。
其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法。但是对于这个题的n,邻接矩阵是存不下的。。。所以默默的抄了一遍大白书上的算法。。。先用kruskal求MST,然后对于MST树,每次询问求p和q的LCA,在求LCA的过程中顺便求出瓶颈路。。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
using namespace std; const int maxn = 50005;
const int maxm = 100010;
const int INF = 1e9;
int n, m;
int fa[maxn], pa[maxn], cost[maxn], L[maxn], anc[maxn][100], maxcost[maxn][100];
struct Edge
{
int to, dist;
};
vector<Edge> edges;
vector<int> G[maxn];
inline void add(int a, int b, int c)
{
edges.PB((Edge){b, c});
edges.PB((Edge){a, c});
int nc = edges.size();
G[a].PB(nc-2); G[b].PB(nc-1);
} inline void init()
{
L[1] = cost[1] = 0;
REP(i, n+1) pa[i] = i;
REP(i, n+1) G[i].clear(); edges.clear();
} int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); } struct E
{
int u, v, w;
bool operator < (const E rhs) const
{
return w < rhs.w;
}
}e[maxm]; void MST()
{
sort(e, e+m);
int cnt = 0;
REP(i, m)
{
int x = findset(e[i].u), y = findset(e[i].v);
if(x != y)
{
pa[x] = y;
cnt++;
add(e[i].u, e[i].v, e[i].w);
}
if(cnt == n-1) return ;
}
} void dfs(int u, int f)
{
fa[u] = f;
int nc = G[u].size();
REP(i, nc)
{
int v = edges[G[u][i]].to, w = edges[G[u][i]].dist;
if(v != f)
{
cost[v] = w;
L[v] = L[u] + 1;
dfs(v, u);
}
}
} void progress()
{
FF(i, 1, n+1)
{
anc[i][0] = fa[i], maxcost[i][0] = cost[i];
for(int j=1; (1 << j) < n; j++) anc[i][j] = -1;
}
for(int j=1; (1 << j) < n; j++) FF(i, 1, n+1)
if(anc[i][j-1] != -1)
{
int a = anc[i][j-1];
anc[i][j] = anc[a][j-1];
maxcost[i][j] = max(maxcost[i][j-1], maxcost[a][j-1]);
}
} int query(int p, int q)
{
int lo;
if(L[p] < L[q]) swap(p, q);
for(lo = 1; (1 << lo) <= L[p]; lo++); lo--; int ans = -INF;
FD(i, lo, 0)
if(L[p] - (1<<i) >= L[q]) ans = max(ans, maxcost[p][i]), p = anc[p][i]; if(p == q) return ans; //LCA -> p FD(i, lo, 0)
if(anc[p][i] != -1 && anc[p][i] != anc[q][i])
{
ans = max(ans, maxcost[p][i]), p = anc[p][i];
ans = max(ans, maxcost[q][i]), q = anc[q][i];
} ans = max(ans, cost[p]);
ans = max(ans, cost[q]);
return ans; //LCA -> fa[q] fa[p]
} int main()
{
int flag = 0;
while(~scanf("%d%d", &n, &m))
{
if(flag++) puts(""); init();
REP(i, m) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w); MST();
dfs(1, -1);
progress(); int Q, p, q;
scanf("%d", &Q);
while(Q--)
{
scanf("%d%d", &p, &q);
printf("%d\n", query(p, q));
}
}
return 0;
}
UVA 11354 Bond(MST + LCA)的更多相关文章
- UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...
- GYM 101889I(mst+lca)
最小生成树上倍增询问裸的. const int maxn = 2e5 + 5; int n, m, q; //图 struct Edge { int u, v; ll cost; bool opera ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
- uva 11354 - Bond(树链拆分)
题目链接:uva 11354 - Bond 题目大意:给定一张图.每次询问两个节点路径上进过边的危急值的最大值的最小值. 解题思路:首先建立最小生成数,然后依据这棵树做树链剖分. #include & ...
- uva 509 RAID!(磁盘数据)
来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...
- 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 642 Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...
- UVA 11354 - Bond (最小生成树 + 树链剖分)
题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...
- UVA 11168 Airport(凸包+直线方程)
题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
随机推荐
- git cheat sheet,git四张手册图
- [Android] ImageView.ScaleType设置图解
ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android ...
- OpenSSL再爆多处高危漏洞
OpenSSL团队于北京时间6月5号晚8点左右发布了5个安全补丁,这次的更新涉及多处高危漏洞,连接:http://www.openssl.org/news/ 受影响的版本包括: OpenSSL 1.0 ...
- ASIFormDataRequest实现post的代码示例
用jquery实现的Post方法可能如下 var param = $.param({ data: JSON.stringify({"from":"234",&q ...
- RGB图像数据字符叠加,图像压缩(ijl库),YUV转RGB
jackyhwei 发布于 2010-01-01 12:02 点击:3218次 来自:CSDN.NET 一些非常有用的图像格式转换及使用的源代码,包括RGB图像数据字符叠加,图像压缩(ijl库),Y ...
- 深入浅出ClassLoader
你真的了解ClassLoader吗? 这篇文章翻译自zeroturnaround.com的 Do You Really Get Classloaders? ,融入和补充了笔者的一些实践.经验和样例.本 ...
- ByteBuffer用法小结
在NIO中,数据的读写操作始终是与缓冲区相关联的.读取时信道(SocketChannel)将数据读入缓冲区,写入时首先要将发送的数据按顺序填入缓冲区.缓冲区是定长的,基本上它只是一个列表,它的所有元素 ...
- IOS PUSH 实践操作~~~~
1.推送过程简介 (1)App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远 ...
- 【LeetCode 209】Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Redrain个人维护并使用的DuiLib和UiLib库源码下载地址
转载请说明原出处:http://blog.csdn.net/zhuhongshu/article/details/40740353,谢谢~~ 我把我自己使用的Duilib库和Uilib库都上传到了Gi ...