[jzyzoj2021]lca模板题
查找最近公共祖先...我也不知道这东西有什么用,在线写法,非常之慢....
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
struct nod{
int y;
int next;
}e[];
int head[]={};
int dep[]={};
int size[]={};
int son[]={};
int top[]={};
int f[]={};
int tot=;
void init(int x,int y){
e[++tot].next=head[x];
head[x]=tot;
e[tot].y=y;
}
void dfs1(int x){
dep[x]=dep[f[x]]+;
size[x]=;
for(int i=head[x];i;i=e[i].next){
if(e[i].y!=f[x]&&!f[e[i].y]){
f[e[i].y]=x;
dfs1(e[i].y);
size[x]+=size[e[i].y];
if(size[son[x]]<size[e[i].y]) son[x]=e[i].y;
}
}
}
void dfs2(int x){
if(x==son[f[x]])top[x]=top[f[x]];
else top[x]=x;
for(int i=head[x];i;i=e[i].next){
if(f[e[i].y]==x) dfs2(e[i].y);
}
}
int ask(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]>dep[top[y]]) x=f[top[x]];
else y=f[top[y]];
}
if(dep[x]<dep[y]) return x;
else return y;
}
int main(){
scanf("%d%d",&n,&m);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
init(x,y);
init(y,x);
}
dfs1();
dfs2();
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
printf("%d\n",ask(x,y));
}
return ;
}
[jzyzoj2021]lca模板题的更多相关文章
- hdu 2586 How far away?(LCA模板题+离线tarjan算法)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2586——How far away ?——————【LCA模板题】
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU2586 How far away ?(LCA模板题)
题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- [hdu 2586]lca模板题(在线+离线两种版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...
- HDU 2586 How far away ? 离线lca模板题
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
随机推荐
- js面向对象的几种常见写法
下面是一个简单的js对象:定义Circle类,拥有成员变量r,常量PI和计算面积的成员函数area(),常用为第一种和第三种. 1.工厂方式 var Circle = function() { var ...
- 关于angularJS的一些用法
AngularJS 事件指令: ng-click/dblclick ng-mousedown/up ng-mouseenter/leave ng-mousemove/over/out ng-keydo ...
- Problem D. Berland Railroads Gym - 101967D (思维)
题目链接:https://cn.vjudge.net/contest/274029#problem/D 题目大意:给你0-9每个数的个数,然后让你找出最大的数,满足的条件是任意三位相连的都能被三整除. ...
- docker ubuntu容器更换阿里源(转)
问题:使用docker 利用下载的ubuntu镜像启动容器时,使用的源下载更新软件的速度较慢. 解决这个问题的方法是跟新ubuntu容器的源 示例:以ubuntu为基础镜像 启动一个名称为 test0 ...
- 工具===激活xmind 8
[下载jar包]: https://stormxing.oss-cn-beijing.aliyuncs.com/files/XMindCrack.jar 方法: 打开xmind 8 安装目录的 ...
- python基础===如何在列表,字典,集合中根据条件筛选数据
#常见的操作如下: data = [1, 5, -3, -2, 6, 0, 9] res = [] for x in data: if x>=0: res.append(x) print(res ...
- 内存不够清理方法,costdown项目时如果裁剪不下来,也可以参考
Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches 频繁的文件访问会导致系统的Cache使用量大增 $ free -m total used free shared ...
- mysql开启GTID跳过错误的方法【转】
1.数据库版本 MySQL> select version() -> ;+-------------------------------------------+| version( ...
- cgic实现输入文件名,打开文件的功能
a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...
- 对cgic的理解——name选项
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "cgic.h" ...