layout: post

title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 最小生成树

- LCA

- 图论

- 训练指南


Bond

UVA - 11354

题意

给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路

题解

先求出最小生成树,然后对这个最小生成树做LCA。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const int logmaxn=20;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
struct LCA{
int n;
int fa[maxn]; ///父亲数组
int cost[maxn]; ///和父亲的费用
int L[maxn]; ///层次(根节点为0)
int anc[maxn][logmaxn]; /// anc[p][i]是结点p的第2^i级父亲。anc[i][0] = fa[i]
int maxcost[maxn][logmaxn]; /// maxcost[p][i]是i和anc[p][i]的路径上的最大费用
void preprocess(){
for(int i=0;i<n;i++){
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++)
for(int i=0;i<n;i++)
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]);
}
}
/// 求p到q的路径上的最大权
int query(int p,int q){
int tmp,log,i;
if(L[p]<L[q])swap(p,q);
for(log=1;(1<<log)<=L[p];log++);log--;
int ans=-inf;
for(int i=log;i>=0;i--)
if(L[p]-(1<<i)>=L[q]){ans=max(ans,maxcost[p][i]);p=anc[p][i];} if(p==q)return ans; ///LCA为p for(int i=log;i>=0;i--)
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[p](它也等于fa[q])
}
};
LCA solver;
int pa[maxn];
int findset(int x){return pa[x]!=x?pa[x]=findset(pa[x]):x;}
vector<int>G[maxn],C[maxn];
void dfs(int u,int fa,int level){
solver.L[u]=level;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v!=fa){
solver.fa[v]=u;
solver.cost[v]=C[u][i];
dfs(v,u,level+1);
}
}
} struct Edge{
int x,y,d;
bool operator <(const Edge& rhs)const{
return d<rhs.d;
}
};
const int maxm=1e5+10;
Edge e[maxm]; int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int kase=0,n,m,x,y,d,Q;
while(cin>>n>>m){
for(int i=0;i<m;i++){
cin>>x>>y>>d;e[i]=(Edge){x-1,y-1,d};
}
sort(e,e+m);
for(int i=0;i<n;i++){pa[i]=i;G[i].clear();C[i].clear();}
for(int i=0;i<m;i++){
int x=e[i].x,y=e[i].y,d=e[i].d,u=findset(x),v=findset(y);
if(u!=v){
pa[u]=v;
G[x].push_back(y);G[y].push_back(x);
C[x].push_back(d);C[y].push_back(d);
}
}
solver.n=n;
dfs(0,-1,0);
solver.preprocess();
if(++kase!=1)cout<<endl;
cin>>Q;
while(Q--){
cin>>x>>y;
cout<<solver.query(x-1,y-1)<<endl;
}
}
return 0;
}

训练指南 UVA - 11354(最小生成树 + 倍增LCA)的更多相关文章

  1. 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

    [题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...

  2. 【bzoj3732】Network 最小生成树+倍增LCA

    题目描述 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 & ...

  3. 训练指南 UVA - 11419(二分图最小覆盖数)

    layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...

  4. 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))

    layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...

  5. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  6. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  7. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  8. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  9. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

随机推荐

  1. hdu6103 Kirinriki(trick+字符串)

    题解: 考虑一开始时,左边从1开始枚举,右边从n开始枚举 我们可以得到一个最大的值k. 但是如果这样依次枚举,复杂度肯定是n^3,是不行的 考虑如何利用上一次的结果,如果我们把1和n同时去掉 就可以利 ...

  2. 浅谈javascript的原型及原型链

    浅谈javascript的原型及原型链 这里,我们列出原型的几个概念,如下: prototype属性 [[prototype]] __proto__ prototype属性 只要创建了一个函数,就会为 ...

  3. 【题解】SDOI2018战略游戏

    被CNST的大小卡了好久.一定要开到18呀…… 首先,遇到这种带各种各样环的图先考虑是不是可以建立圆方树,在圆方树上求出答案.然后转化为圆方树之后,我们就将图转化到了树上.答案非常的明显:只要一个圆点 ...

  4. BZOJ2115 [Wc2011] Xor 【线性基】

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 3915  Solved: 1633 [Submit][Stat ...

  5. jocky1.0.3 (原joc) java混淆器 去除jdk版本限制

    昨晚下班回去,研究了下jocky1.0.3的使用,发现编译时提示引用类库版本不对,捣弄了半个小时后终于理解,原来是我的jdk1.7版本过高,这货是06年的版本,到现在都没更新过,支持(限制)的最高版本 ...

  6. Ubuntu下使用find / -name aaa* 提示“find: 路径必须在表达式之前: XXXX”

    在使用find命令查找文件时,出现了如题所示的错误提示,因为之前都是这样用的,也没出过错,这次语法都是一样的居然不行了,很是纳闷. 后来了解到如下的情况: 如果当前所在目录存在要查找的目标文件时会出现 ...

  7. 使用adobe pdf去除PDF文档中的批量雷同文本

    一.问题的提出 MgoSoft tiff to pdf软件没有提供中国地区的非VISA用户的购买渠道,中国通常都是银联标识走天下,卡不是VISA买不了这样的软件, 那么, MgoSoft tiff t ...

  8. C. Annoying Present SB题

    C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. 7月18号day10总结

    今天学习过程和小结 今天学会了用git从GitHub上克隆代码然后打包成jar包,然后在idea程序中引入这个jar包的依赖来使用jar包中的程序. 通过这个中的网址: 在Git Bash Here中 ...

  10. 动态规划:LCS

    先上状态转移方程,还是很容易看明白的 例题是Codevs的1862,这个题不是实现了方程就可以了的,还要完成一个事情那就是计数,数一数到底有多少个最长公共子序列 #include<cstdio& ...