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

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

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

  2. GYM 101889I(mst+lca)

    最小生成树上倍增询问裸的. const int maxn = 2e5 + 5; int n, m, q; //图 struct Edge { int u, v; ll cost; bool opera ...

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

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

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

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

  5. uva 509 RAID!(磁盘数据)

    来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...

  6. 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 642  Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...

  7. UVA 11354 - Bond (最小生成树 + 树链剖分)

    题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...

  8. UVA 11168 Airport(凸包+直线方程)

    题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...

  9. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

随机推荐

  1. git版本库底层命令

    当我们在使用git的时候,有时候需要知道当前文件夹相对于工作目录根目录的相对路径等等,那么我们可以使用 git rev-parse 添加一个参数就可以实现,如: 显示当前仓库版本库 .git 目录所在 ...

  2. UVa 120 Stacks of Flapjacks【构造法】

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...

  3. 自定义View,圆形头像

    1. 效果图 2. xml中 <com.etoury.etoury.ui.view.CircleImg android:id="@+id/user_info_head_img" ...

  4. vs2013编译boost库

    打开vs2013>>visual studio tools>>VS2013 x64 本机工具命令提示 cd D:\lib\boost_1_55_0\boost_1_55_0 b ...

  5. DbgPrint输出格式 Unicodestring

    DbgPrint输出格式 Unicodestring  1) 直接打印字符串. DbgPrint("Hello World!"); 2) 空结尾的字符串,你可以用普通得C语法表示字 ...

  6. LINUX启动ORACLE监听和服务

    可通过secureCRT或者telnet直接连 启动监听命令:lsnrctl start 成功启动后:sqlplus /nolog 回车 conn / as sysdba 回车 startup 回车 ...

  7. 【转】linux中wait与waitpid的差别

    原文网址:http://blog.163.com/libo_5/blog/static/15696852010324287748/ zombie不占用内存也不占用CPU,表面上我们可以不用在乎它们的存 ...

  8. 强大的日志分析工具 -- NSLogger

    转:http://www.cnblogs.com/yingkong1987/p/3329945.html 强大的日志分析工具 -- NSLogger 源码:https://github.com/fpi ...

  9. C#+AE 用MapControl加载栅格格式文件

    需要引入DataSourceRaster命名空间. 具体步骤如下: ①:利用工作控件工厂抽象类定义接口变量,利用工作空间工厂的子类RatserWorkspaceFactory对应的组件类RasterW ...

  10. JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码

    本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 提升到 20 (提升了 7 倍) ...