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. C++——继承时的this指针

    1.this指针只在类的成员函数中使用,当类的成员函数需要用到自己的指针时就要用到this指针.但静态函数不能使用this关键字,其解释是:因为this是个引用,哪个对象调用方法就引用哪个对象. 而静 ...

  2. MFC随机数

    void CMFCDemoDlg::OnClickedGetrand() { wchar_t str[]; //srand((unsigned)time(NULL)); int num = rand( ...

  3. C语言的getopt

    By francis_hao    Jul 5,2017   getopt:分析命令行选项 概述 #include <unistd.h>int getopt(int argc, char ...

  4. OpenJudge百炼-2747-数字方格-C语言-枚举

    描述:如上图,有3个方格,每个方格里面都有一个整数a1,a2,a3.已知0 <= a1, a2, a3 <= n,而且a1 + a2是2的倍数,a2 + a3是3的倍数, a1 + a2 ...

  5. SICAU-OJ:要我唱几首歌才能够将你捕捉

    要我唱几首歌才能够将你捕捉 题意: 有N种颜色的牛,现在可以执行以下两种操作: 1.抓捕一只牛,代价为ai: 2.花费x的代价使用魔法,让所有颜色加1,N会变为1. 求得到N种颜色的牛最少花费的代价. ...

  6. jquery实现通用结构折叠面板效果

    效果截图: 说明:可以任意添加多个类似结构样式,点击标题栏图片对应隐藏.显示. jquery代码: 思路一:基本方法 <script src="http://apps.bdimg.co ...

  7. 关于javascript中的this 一段小实例深有体会啊

    先声明鄙人正在努力的把脚抬进门来,说的都是比较粗浅的知识,但都是我实践中得出的体会,很深刻.  正在自学中挣扎的DOG. 先看段代码: function highlightRows() { if(!d ...

  8. POJ 3617 Best Cow Line (模拟)

    题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...

  9. [bzoj1588][HNOI2002]营业额统计——splay

    题目大意 你被要求编写一个数据结构,支援以下操作,操作在线. 插入一个元素 查询一个元素与之前插入元素的最小差值. 题解 一道模板题.我是写了一个pre和succ函数水过的.1A,比较高兴. 代码 # ...

  10. 【IDEA】设置类头注释和方法注释

    idea和eclipse的注释还是有一些差别的. 类头注释: 打开file->setting->Editor->Filr and Code Templates->Include ...