洛谷P1967货车运输——倍增LCA
题目:https://www.luogu.org/problemnew/show/P1967
就是倍增LCA的裸题,注意一些细节即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const MAXN=,MAXM=,inf=1e9;
int n,m,q,dep[MAXN],pre[MAXN][],mn[MAXN][],ct1,ct=,head[MAXN];
int fa[MAXN],ans;
struct N{
int hd,to,next,w;
N(int h=,int t=,int n=,int w=):hd(h),to(t),next(n),w(w) {}
}edge[MAXM<<],ed[MAXM<<];
int find(int x)
{
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
void add(int x,int y,int z)
{
edge[++ct]=N(x,y,head[x],z);head[x]=ct;
edge[++ct]=N(y,x,head[y],z);head[y]=ct;
}
void add1(int x,int y,int z)
{
ed[++ct1]=N(x,y,,z);
}
bool cmp(N x,N y){return x.w>y.w;}
void kruskal()
{
sort(ed+,ed+ct1+,cmp);
for(int i=,u,v;i<=ct1;i++)
{
u=ed[i].to;
v=ed[i].hd;
int a=find(u);
int b=find(v);
if(a!=b)
{
add(u,v,ed[i].w);
fa[a]=b;
}
}
}
void dfs(int x,int f)
{
dep[x]=dep[f]+;
pre[x][]=f;
for(int i=head[x],u;i;i=edge[i].next)
{
u=edge[i].to;
if(u==f)continue;
mn[u][]=edge[i].w;
dfs(u,x);
}
}
void lca(int x,int y)
{
int ans=inf;
if(dep[x]>dep[y])swap(x,y);
int d=dep[y]-dep[x];
for(int i=;i<=;i++)
if((d>>i)&)ans=min(ans,mn[y][i]),y=pre[y][i];//不是i-1!
if(x==y)
{
printf("%d\n",ans);
return;
}
for(int i=;i>=;i--)//不是i>0
{
if(pre[x][i]!=pre[y][i])
{
ans=min(ans,min(mn[x][i],mn[y][i]));
x=pre[x][i];y=pre[y][i];
}
}
ans=min(ans,min(mn[x][],mn[y][]));
printf("%d\n",ans);
}
int main()
{
int x,y,z;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&x,&y,&z),add1(x,y,z);
for(int i=;i<=n;i++)fa[i]=i;
kruskal();
for(int i=;i<=n;i++)
if(!dep[i])dfs(i,);
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
{
pre[i][j]=pre[pre[i][j-]][j-];
mn[i][j]=min(mn[i][j-],mn[pre[i][j-]][j-]);
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&x,&y);
if(find(x)!=find(y))printf("-1\n");
else lca(x,y);
}
return ;
}
洛谷P1967货车运输——倍增LCA的更多相关文章
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- 洛谷 P1967 货车运输
洛谷 P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在 ...
- 【杂题总汇】NOIP2013(洛谷P1967) 货车运输
[洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...
- [洛谷 P1967] 货车运输 (最大生成树 lca)
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- 洛谷 P1967 货车运输 LCA + 最小生成树
两点之间边权最大值的最小值一定在图的最小生成树中取到. 求出最小生成树,进行倍增即可. Code: #include<cstdio> #include<algorithm> u ...
- 洛谷 P1967 货车运输(克鲁斯卡尔重构树)
题目描述 AAA国有nn n座城市,编号从 11 1到n nn,城市之间有 mmm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qqq 辆货车在运输货物, 司机们想知道每辆车在不超过车 ...
- [NOIP2013] 提高组 洛谷P1967 货车运输
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输
https://www.luogu.org/problem/show?pid=1967#sub || http://www.cogs.pro/cogs/problem/problem.php?pi ...
随机推荐
- 我的Android进阶之旅------>怎样解决Android 5.0中出现的警告: Service Intent must be explicit:
我的Android进阶之旅-->怎样解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...
- vue2.0 flexible.js + rem 进行自适应开发
1.在页面中引入flexible.js base.js /** * flexible.js 阿里前端自适应解决方案 */ ;(function(win, lib) { var doc = win.do ...
- C语言-二维背包问题
二维费用背包问题 问题: 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有 一个可付出的最大值(背包容量).问怎样选择物品可以得到最大的价 ...
- node.js如何读取MySQL数据
先安装mysql模块. node.js默认安装时,模块文件放在 /usr/local/lib/node_modules 这个目录下,为了便宜管理,模块还是统一安装到这里好. $ cd /usr/loc ...
- IO模式——同步(堵塞、非堵塞)、异步
为什么IO模式非常重要?由于现代的计算机和操作系统的架构决定了CPU是稀缺资源,大家都要来一起竞争.而IO(特别是网络相关的IO)的速度往往较慢.所以怎样进行IO就有了多种模式,包含同步.异步.堵塞. ...
- how to run a continuous background task on OpenShift
https://stackoverflow.com/questions/27152438/best-way-to-run-rails-background-jobs-with-openshift ht ...
- Linux驱动经典面试题目
1. linux驱动分类 2. 信号量与自旋锁 3. platform总线设备及总线设备怎样编写 4. kmalloc和vmalloc的差别 5. module_init的级别 6. 加入 ...
- dsoframer注冊说明及在VC2010使用
一.dsoframer在XP.win7和win8中的注冊方法. 从微软站点下载DsoFrmaer_KB311765_x86.exe,双击解开后得到的dsoframer.ocx等文件. (一)XP注冊 ...
- Chef vs Puppet vs Ansible vs Saltstack: Which Works Best For You?
Ansible vs SaltStack 谁才是自动化运维好帮手? - CSDN博客 https://blog.csdn.net/a105421548/article/details/53558598 ...
- flume topology design . tier num 分层数目
32:+:1 x:1 x<=8 https://flume.apache.org/FlumeUserGuide.html#flume-topology-design Flume topology ...