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. Huge CSV and XML Files in Python, Error: field larger than field limit (131072)

    Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...

  2. Android UI学习 - FrameLayou和布局优化(viewstub)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/308090 Fram ...

  3. Codeforces 383A - Milking cows

    原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...

  4. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

  5. TCP协议的一些问题

    1 连接握手三次 解释1:已失效的连接请求报文段 情况下如下:client发出的第一个连接请求报文段并没有丢失,而是在某个网络结点长时间的滞留了,以致延误到连接释放以后的某个时间才到达server.本 ...

  6. iOS-利用AFNetworking(AFN 1.x)-实现文件上传

    转:http://www.kaifazhe.com/ios_school/380067.html 官方建议AFN的使用方法 1. 定义一个全局的AFHttpClient:包含有 1> baseU ...

  7. .net-.net试题2

    ylbtech-doc:.net-.net试题2 .net试题2 1.A,.net试题2返回顶部 01.{DotNet题目}下列变量的赋值,正确的是:(  )(选择2项) A )int myInteg ...

  8. WebView介绍

    本文主要对WebView进行介绍,包括webView 4个可以定制的点.设置WebView back键响应.控制网页的链接仍在webView中跳转.显示页面加载进度.处理https请求.利用addJa ...

  9. 浅析基层检察院派驻乡镇检察室的健康发展 z

    时间:2011-03-22 10:08 作者:祝志方 新闻来源:正义网 一.前言 在我国,基层检察院派驻乡镇检察室的发展经过了一个曲折发展的历程,上世纪80年代,随着经济社会的发展,一批乡镇检察室应运 ...

  10. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...