luogu 1967 货车运输(最大生成树+LCA)
题意:给出一颗n个点的图,q个询问,每次询问u到v的路径中最小的边最大是多少。
图的最大生成树有一个性质,对于该图的任意两个点,在树中他们之间路径的最小边最大。
由于这个图不一定联通,于是我们对它的联通块都求一次最大生成树。
每次询问就变成了在最大生成树上找出u到v路径的最小边。
这个显然可以用LCA维护点到它的2^x祖先之间的边的最小值来解决。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... const int DEG=;
struct Edge{int p, next, w;}edge[N<<];
struct Node{int u, v, w;}node[N*];
int head[N], cnt=, F[N], from[N];
int fa[N][DEG], mi[N][DEG], deg[N];
queue<int>que; void add_edge(int u, int v, int w){edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;}
bool comp(Node a, Node b){return a.w>b.w;}
int find(int x){return F[x]==-?x:F[x]=find(F[x]);}
void Kruscal(int n){
mem(F,-); sort(node+,node+*n+,comp);
FOR(i,,*n) {
int u=node[i].u, v=node[i].v, t1=find(u), t2=find(v);
if (t1!=t2) add_edge(u,v,node[i].w), add_edge(v,u,node[i].w), F[t1]=t2;
}
}
void BFS(int root){
deg[root]=; fa[root][]=root; mi[root][]=INF; que.push(root);
while (!que.empty()) {
int tmp=que.front(); que.pop();
FO(i,,DEG) fa[tmp][i]=fa[fa[tmp][i-]][i-], mi[tmp][i]=min(mi[tmp][i-],mi[fa[tmp][i-]][i-]);
for (int i=head[tmp]; i; i=edge[i].next) {
int v=edge[i].p;
if (v==fa[tmp][]) continue;
deg[v]=deg[tmp]+; fa[v][]=tmp; mi[v][]=edge[i].w; que.push(v);
}
}
}
int LCA(int u, int v){
int ans=INF;
if (deg[u]>deg[v]) swap(u,v);
int hu=deg[u], hv=deg[v], tu=u, tv=v;
for (int det=hv-hu, i=; det; det>>=, ++i) if (det&) ans=min(ans,mi[tv][i]), tv=fa[tv][i];
if (tu==tv) return ans;
for (int i=DEG-; i>=; --i) {
if (fa[tu][i]==fa[tv][i]) continue;
ans=min(ans,mi[tu][i]); ans=min(ans,mi[tv][i]);
tu=fa[tu][i]; tv=fa[tv][i];
}
return min(ans,min(mi[tu][],mi[tv][]));
}
int main ()
{
int n, m, q, u, v;
scanf("%d%d",&n,&m);
FOR(i,,m) scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].w);
Kruscal(m);
int pos=;
FOR(i,,n) {
int v=find(i);
if (from[v]==) from[v]=, BFS(v);
}
scanf("%d",&q);
while (q--) {
scanf("%d%d",&u,&v);
if (find(u)!=find(v)) {puts("-1"); continue;}
printf("%d\n",LCA(u,v));
}
return ;
}
luogu 1967 货车运输(最大生成树+LCA)的更多相关文章
- [luogu 1967]货车运输
货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情 ...
- kruskal - 倍增 - 并查集 - Luogu 1967 货车运输
P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过 ...
- 【NOIP2013】货车运输 最大生成树+LCA
题目描述 AA国有nn座城市,编号从 1到n,城市之间有m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重 ...
- LUOGU P1967 货车运输(最大生成树+树剖+线段树)
传送门 解题思路 货车所走的路径一定是最大生成树上的路径,所以先跑一个最大生成树,之后就是求一条路径上的最小值,用树剖+线段树,注意图可能不连通.将边权下放到点权上,但x,y路径上的lca的答案不能算 ...
- Luogu P1967 货车运输(Kruskal重构树)
P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...
- codevs3287货车运输(最小生成树+LCA)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 ...
- P1967 货车运输(倍增LCA,生成树)
题目链接: https://www.luogu.org/problemnew/show/P1967 题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制, ...
- TZOJ 4848 货车运输(最大生成树+倍增lca)
描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多 ...
- $Noip2013/Luogu1967$ 货车运输 最大生成树+倍增$lca$
$Luogu$ $Sol$ 首先当然是构建一棵最大生成树,然后对于一辆货车的起点和终点倍增跑$lca$更新答案就好.记得预处理倍增的时候不仅要处理走了$2^i$步后是那个点,还有这中间经过的路径权值的 ...
随机推荐
- 【LG3238】 [HNOI2014]道路堵塞
题目描述 给你一张\(N\)个点.\(M\)条边的有向图,按顺序给定你一条有\(L\)条边的\(1\rightarrow n\)的最短路, 每次断掉这\(L\)条边中的一条(不对后面答案产生影响),求 ...
- 搜索引擎ElasticSearch系列(一): ElasticSearch2.4.4环境搭建
一:ElasticSearch简介 Elasticsearch is a distributed, RESTful search and analytics engine capable of sol ...
- js显示对象所有属性和方法的函数
function ShowObjProperty2( obj ) { // 用来保存所有的属性名称和值 var attributes = '' ; var methods = '' // 开始遍历 f ...
- React入门基础(学习笔记)
这篇博客是我通过阅读React官方文档的教程总结的学习笔记,翻译可能存在误差,如有疑问请参见http://reactjs.cn/react/docs/tutorial.html . 一.所需文件 在编 ...
- scrapy (一)
scrapy框架 scrapy 是一个爬虫框架,能够高效率,高层次的爬取页面的数据并进行处理. 在scrapy的英文文档中,有这样的一个流程图 scrapy 框架主要分为五大部分,spider, en ...
- Linux大全
Linux 基本指令介紹 一定要先學會的指令:ls, more, cd, pwd, rpm, ifconfig, find 登入與登出(開機與關機):telnet, login, exit, sh ...
- 001 -js对时间日期的排序
001-JS对时间日期的排序 最近在做公司的项目时间,产品给了一个很简单的页面,让帮忙写一下.首先看一下产品的需求: 需要对该列表进行排序 思路:(1)可以在数据库写sql语句的时间直接一个DESC按 ...
- 【转】AOE机制的DSL及其实际运用
AOE这个词的意思,我相信玩过WOW的人都不陌生,包括玩过LoL的也不会陌生,说穿了就是一个区域内发生效果(Area of effect).这里我们要讨论的就是关于一个适合于几乎所有游戏的AOE机制, ...
- Paper Reading - Convolutional Sequence to Sequence Learning ( CoRR 2017 ) ★
Link of the Paper: https://arxiv.org/abs/1705.03122 Motivation: Compared to recurrent layers, convol ...
- 转:为什么说招到合适的人比融到钱更加重要 - Hiring Great Talent is More Important Than Fund Raising
我在猎头行业工作了 20 多年,一直在帮助创业公司招聘优秀的人才.我服务过的客户既有 VC 投资的初创企业,也有即将 IPO 的公司.我和 200 多个 VC 合作过,也见过 300 多个客户失败的案 ...