[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 ...
随机推荐
- Sqlmap注入技巧收集整理
TIP1 当我们注射的时候,判断注入 http://site/script?id=10http://site/script?id=11-1 # 相当于 id=10http://site/script? ...
- udhcpc命令
要使用网络通讯,所以不可避免的要用到dhcp.理想的网络通讯方式是下面3种都要支持: 1,接入已有网络.这便要求可以作为dhcp客户端. 2,作为DHCP服务器,动态分配IP. 简单说下前2种情况. ...
- Centos 6.4搭建git服务器【转】
前阵子公司需要,让我搭个Git服务器,把之前用的SVN上代码迁移到git上去,所以就在阿里云主机上搭了一个,记录了下安装过程,留存文档以备查阅.本篇本章只涉及搭建部分的操作,更多git的使用可以参考文 ...
- weblogic12.1.3 静默安装 建域
--安装依赖包 yum -y install compat-libcap1 compat-libstdc++ gcc gcc-c++ glibc-devel libaio-devel libstdc+ ...
- 在Linux上安装pycharm
1.首先在官网下载pycharm并进行提取,将提取的文件夹放在/usr下面(或者任意位置) 2.然后vi /etc/hosts 编辑 将0.0.0.0 account.jetbrains.com添加到 ...
- Python模块之pxssh
pxssh模块用于在python中ssh远程连接,执行命令,返回结果,但注意不支持Windows系统 #!/usr/bin/env python #-*- coding:utf-8 -*- from ...
- docker swarm join 报错
[peter@minion ~]$ docker swarm join --token SWMTKN-1-3mj5po3c7o04le7quhkdhz6pm9b8ziv3qe0u7hx0hrgxsna ...
- Funny Car Racing(最短路变形)
描述 There is a funny car racing in a city with n junctions and m directed roads. The funny part is: e ...
- Mysql的刷脏页问题
平时的工作中,不知道你有没有遇到过这样的场景,一条 SQL 语句,正常执行的时候特别快,但是有时也不知道怎么回事,它就会变得特别慢,并且这样的场景很难复现,它不只随机,而且持续时间还很短. 当内存数据 ...
- fastdfs5.11+centos7.2 按照部署(三)【转载】
1.测试 前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解.FastDFS的基础模块都搭好了,现在开始测试下载. 1.1 配置客户端 同样的,需要修改客户端的配置文件: vim /etc ...