洛谷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 ...
随机推荐
- 谈一次Linux的木马攻击数据爆满造成的Mysql无法启动
起初以为是mysql它们之间的扩展没有开启! 后来发现,木马的确使它初始化了,最开始没有用图形化界面 而后,修改并且开启所有pdo扩展 VIM基本操作(除了插入,其它的命令前提是按ESC): 插入: ...
- Java中HashMap遍历的两种方法(转)
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- Unsupported major.minor version (jdk版本错误)解决方案 办法
如果你遇到了 Unsupported major.minor version ,请认真看一下,说不定会有帮助. 我花两个小时总结的经验,你可能10分钟就得到了. ^**^ 一.错误现象: 当改变了jd ...
- 整理自Git文件夹下资料及man手册(不包括书籍)
$ git commit -awhich will automatically notice any modified (but not new) files, add them to the ind ...
- centos7 设置网络
https://lintut.com/how-to-setup-network-after-rhelcentos-7-minimal-installation/ First, type “nmcli ...
- 用Cocoapods集成XMPPFramework 遇 Module 'KissXML' not found 问题
用Coacopods集成XMPPFramework完成后Command + B,报Module 'KissXML' not found 一般来说,通过Coacopods集成集成第三方框架,不会再有依赖 ...
- 1185: [HNOI2007]最小矩形覆盖
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1426 Solve ...
- xorm
https://github.com/go-xorm/xorm Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3, ...
- tornado安全应用之cookie
目前大多数服务器判断用户是否登录一般通过session机制,Tornado 通过 set_secure_cookie 和 get_secure_cookie 方法直接支持了这种功能.原理类似于sess ...
- 调用远程service aidl接口定义
Android studio 查看aidl定义的文件:当你进入你的AIDL文件并编写好了之后,点击AS上方菜单栏中的Build->Make Project,之后便可以在当前工程的app/buil ...