文章目录

  • 最近公共祖先
  • 树上距离
  • 距离查询

以下代码使用的是倍增算法求lca

最近公共祖先

poj1330

#include<bits/stdc++.h>
using namespace std;
const int N=10010;
const int M=20010;
int h[N], ne[M], to[M];
int cnt, root;
int fa[N][17];
void add(int a, int b)
{to[cnt] = b,ne[cnt] = h[a],h[a] = cnt++;
}
queue<int> q;
int depth[N];
void bfs()
{memset(depth,-1,sizeof(depth));depth[root] = 1;int now = root;q.push(now);while (!q.empty()){now = q.front();q.pop();for (int i = h[now]; i != -1; i = ne[i]){int j = to[i];if(depth[j]!=-1) continue;depth[j] = depth[now] + 1;q.push(j);fa[j][0] = now;for (int x = 1; x <= 16; x++){fa[j][x] = fa[fa[j][x - 1]][x - 1];}}}
}
int lca(int x, int y)
{if (depth[x] < depth[y])swap(x, y);for (int i = 16; i >= 0; i--){if (depth[fa[x][i]] >= depth[y]){x=fa[x][i];}}if(x==y) return y;for( int i=16;i>=0;i--){if(fa[x][i]!=fa[y][i]){x=fa[x][i],y=fa[y][i];}}return fa[x][0];
}
void solve( ){memset(h,-1,sizeof h);memset(fa,0,sizeof(fa));//初始化为0int n,x,y;cin>>n;for( int i=1;i<n;i++){scanf("%d%d",&x,&y);add(x,y),add(y,x);fa[y][0]=x;}root=1,cnt=0;while(fa[root][0]!=-1){root=fa[root][0];}bfs();scanf("%d%d",&x,&y);cout<<lca(x,y)<<'\n';}
int main(){int t;cin>>t;while(t--){solve();}return 0;
}

树上距离

hdu2586

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=40010;
const int M=N+N;
int h[N], ne[M], to[M],val[M];
int cnt, root;
int fa[N][17];
ll dis[N][17];
void add(int a, int b,int c)
{val[cnt]=c,to[cnt] = b,ne[cnt] = h[a],h[a] = cnt++;
}
queue<int> q;
int depth[N];
void bfs()
{memset(depth,-1,sizeof(depth));depth[root] = 1;int now = root;q.push(now);while (!q.empty()){now = q.front();q.pop();for (int i = h[now]; i != -1; i = ne[i]){int j = to[i];if(depth[j]!=-1) continue;depth[j] = depth[now] + 1;q.push(j);fa[j][0] = now;dis[j][0]=val[i];for (int x = 1; x <= 16; x++){fa[j][x] = fa[fa[j][x - 1]][x - 1];dis[j][x]=dis[fa[j][x-1]][x-1]+dis[j][x-1];}}}
}
int lca(int x, int y)
{long long res=0;if (depth[x] < depth[y])swap(x, y);for (int i = 16; i >= 0; i--){if (depth[fa[x][i]] >= depth[y]){res+=dis[x][i];x=fa[x][i];}}for( int i=16;i>=0;i--){if(fa[x][i]!=fa[y][i]){res+=dis[x][i]+dis[y][i];x=fa[x][i],y=fa[y][i];}}if(x!=y){res+=dis[x][0]+dis[y][0];}return res;
}
void solve( ){int n,m,x,y,z;    memset(h,-1,sizeof h);memset(fa,0,sizeof(fa));//初始化为0cin>>n>>m;for( int i=1;i<n;i++){scanf("%d%d%d",&x,&y,&z);add(x,y,z),add(y,x,z);}root=1,cnt=0;bfs();for( int i=0;i<m;i++){scanf("%d%d",&x,&y);cout<<lca(x,y)<<'\n';}}
int main(){int t;cin>>t;while(t--){solve();}return 0;
}

距离查询

#include <algorithm> //STL通用算法
#include <cmath>//定义数学函数
#include <complex> //复数类
#include <cstdio>//定义输入/输出函数
#include <cstdlib>//定义杂项函数及内存分配函数
#include <cstring>//字符串处理
#include <deque> //STL双端队列容器
#include <map> //STL 映射容器
#include <iostream>//数据流输入/输出
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream>//基于字符串的流
#include <stack> //STL堆栈容器
#include <string>//字符串类
#include <vector>//STL动态数组容器
using namespace std;
typedef long long ll;
const int N=40010;
const int M=N+N;
int h[N], ne[M], to[M],val[M];
int cnt, root;
int fa[N][17];
ll dis[N][17];
void add(int a, int b,int c)
{val[cnt]=c,to[cnt] = b,ne[cnt] = h[a],h[a] = cnt++;
}
queue<int> q;
int depth[N];
void bfs()
{memset(depth,-1,sizeof(depth));depth[root] = 1;int now = root;q.push(now);while (!q.empty()){now = q.front();q.pop();for (int i = h[now]; i != -1; i = ne[i]){int j = to[i];if(depth[j]!=-1) continue;depth[j] = depth[now] + 1;q.push(j);fa[j][0] = now;dis[j][0]=val[i];for (int x = 1; x <= 16; x++){fa[j][x] = fa[fa[j][x - 1]][x - 1];dis[j][x]=dis[fa[j][x-1]][x-1]+dis[j][x-1];        }}}
}
int lca(int x, int y)
{int res=0;if (depth[x] < depth[y])swap(x, y);for (int i = 16; i >= 0; i--){if (depth[fa[x][i]] >= depth[y]){res+=dis[x][i];x=fa[x][i];}}// cout<<x<<y<<endl;// if(x==1&&y==1) cout<<dis[35][3]<<"????"<<endl<<endl;for( int i=16;i>=0;i--){if(fa[x][i]!=fa[y][i]){res+=dis[x][i]+dis[y][i];x=fa[x][i],y=fa[y][i];}}if(x!=y){res+=dis[x][0]+dis[y][0];}return res;
}
void solve( ){//FILE *fp=fopen("test1.out","w");int n,m,x,y,z;    memset(h,-1,sizeof(h));memset(fa,0,sizeof(fa));cin>>n;for( int i=0;i<n-1;i++){scanf("%d%d%d",&x,&y,&z);add(x,y,z),add(y,x,z);}root=1,cnt=0;bfs();cin>>m;for( int i=0;i<m;i++){scanf("%d%d",&x,&y);cout<<lca(x,y)<<'\n';// fprintf(fp,"%d\n",lca(x,y));}}
int main(){int t=1;while(t--){solve();}return 0;
}

buct寒假集训——lca相关推荐

  1. DP\记忆化搜索-牛客寒假集训营3-牛牛的DRB迷宫I

    DP-牛客寒假集训营3-牛牛的DRB迷宫I 题目: 题意: 求迷宫问题的方案数量.与--求迷宫问题的方案数量.与--求迷宫问题的方案数量.与--DP题型总结中的<摘花生>类似.中的< ...

  2. 构造-牛客寒假集训营3-牛牛的DRB迷宫II

    构造-牛客寒假集训营3-牛牛的DRB迷宫II 题目: 题意: 输入一个数字,表示从起点(1,1)到终点(n,m)的方案数量,输出满足条件的迷宫.输入一个数字,表示从起点(1,1)到终点(n,m)的方案 ...

  3. 关于构造和二进制,题目:牛牛的DRB迷宫Ⅱ(源自牛客竞赛2020年寒假集训)

    关于构造和二进制,题目:牛牛的DRB迷宫Ⅱ(源自牛客竞赛2020年寒假集训) 题目: 链接:https://ac.nowcoder.com/acm/contest/3004/B 来源:牛客网 题目描述 ...

  4. 2019NEFU寒假集训新生考试 2020.1.6

    2019NEFU寒假集训新生考试 2020.1.6 为期一周的的培训终于面临尾声,就以一场考试告终吧. A 28的因子 Description 我们都知道28的因子中含有4和7,而某些人偏爱数字4和7 ...

  5. 大一寒假集训(11)(12)---map,set

    大一寒假集训(11)-map 1.查字典 nefu 1678 #include <bits/stdc++.h> using namespace std; map<string,int ...

  6. 寒假集训三(暴力枚举)2020.01.02(11题)

    寒假集训三(暴力枚举)id :521 Problem:A 二倍的问题 Description 给定2到15个不同的正整数,你的任务是计算这些数里面有多少个数对满足:数对中一个数是另一个数的两倍.比如给 ...

  7. 2023寒假集训通知

    各位家长,各位同学,新年好! 过去的2022是我们编程学习,算是比较成功的一年.大家跟着我们的团队进行了约一年的培训,很有收获.纵观全年: 1.寒假我们认真集训了24天 2.三四月间参加了人工智能学会 ...

  8. 2019寒假集训新生考试

    序言: 为期一周的欢乐的寒假集训终于结束了,很高兴能认识这么多的大佬和学长.学姐.很感谢ljw学长和陈宇老师为我们寒假培训所作出的贡献. 简单点评一下今天的考试试题,就在比赛前的中午,jlw学长说题目 ...

  9. 2019寒假集训新生考试 【持续更新中】

    2019寒假集训新生考试-NEFU 0107 emoji表情包 Problem A nefu 2101 28的因子 ⭐️ 本题使用暴力枚举,统计n可以分成几个4和7, 为了防止TLE ,先统计4的个数 ...

最新文章

  1. RedHat 7.0及CentOS 7.0禁止Ping的三种方法
  2. windows下客户端连接上马上会断开连接_浅尝Java NIO与Tomcat简单连接调优
  3. Spring Boot + Thymeleaf 创建web项目
  4. Android Google Play app signing 最终完美解决方式
  5. postman调用webservice接口_【分享】关于接口对前后端和测试的意义
  6. 单机按钮来图片轮播_原生js如何实现轮播图效果?
  7. 【深度学习】嵌入式人工智能概述
  8. flashfxp3.41中文版注册码:(适合最新版本)
  9. 基于设备树的TQ2440触摸屏驱动移植
  10. win7休眠设置在哪里_win7怎么开启休眠模式
  11. 打开win10电脑的蓝牙
  12. FPGA零基础学习:数字通信中的电压标准
  13. [Java]jhsdb查看内存内Java对象
  14. myeclipse 6.5 注册码 myeclipse 6.0 注册码
  15. 51单片机(八).单片机的I2C与串口通讯
  16. JS如何区分微信浏览器、QQ浏览器和QQ内置浏览器,解决 ios 无法判断是否为qq浏览器环境的问题。
  17. Zookeeper实现服务注册发现
  18. 浅谈自然辩证在现代科学领域的作用
  19. Swing版《房屋租赁合同》
  20. cmd常用命令大全,写批处理bat文件使用

热门文章

  1. poi多个模板实现文档合并
  2. My $650,100 Lunch with Warren Buffett
  3. Java实现窗口框架,转换金额的大小写
  4. java故事之一年又一年
  5. 大一寒假集训(11)(12)---map,set
  6. Apache Hudi 详解
  7. unity之环状图片轮播
  8. Crossplane - 比 Terraform 更先进的云基础架构管理平台?
  9. java智能算法--机器学习包
  10. 三个和尚没水喝的启发