LCA最近公共祖先(POJ1330)
题目链接:http://poj.org/problem?id=1330
解题报告:
先将一个子节点,深搜每一个根节点,并标记。
然后深索另一个子节点,当发现访问过了,就找到了最近的公共祖先。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int maxn = ; bool vis[maxn];
int father[maxn]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(father,-,sizeof(father));
memset(vis,,sizeof(vis)); int n;
scanf("%d",&n);
int a,b;
for(int i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
father[b]=a;
} scanf("%d%d",&a,&b);
while(b!=-)
{
vis[b]=true;
b=father[b];
} while(!vis[a])
a=father[a];
printf("%d\n",a); }
return ;
}
LCA最近公共祖先(POJ1330)的更多相关文章
- lca 最近公共祖先
http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- LCA(最近公共祖先)模板
Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...
- CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )
CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...
- LCA近期公共祖先
LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...
- LCA 近期公共祖先 小结
LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...
- poj1330 lca 最近公共祖先问题学习笔记
首先推荐两个博客网址: http://dongxicheng.org/structure/lca-rmq/ http://scturtle.is-programmer.com/posts/30055. ...
- LCA最近公共祖先 ST+RMQ在线算法
对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决. 这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...
- Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】
一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...
随机推荐
- Wireshark抓取TCP包分析
介绍 本篇文章是使用wireshrak对某个https请求的tcp包进行分析. 目的 通过抓包实际分析了解tcp包. 准备工作 在我自己机子上安装的是wireshark2.2.6版本,随机查找了某个T ...
- SQL server数据库端口访问法
最近数据库连接,也是无意中发现了这个问题,数据库可根据端口来连接 我用的是sql2014测试的,在安装其他程序是默认安装了sql(sql的tcp/ip端口为xxx),服务也不相同,但是由于比较不全,我 ...
- args 、kwargs不定参数通过列表、元组、字典传递
*args .**kwargs是不定参数的表达方法,一般用于函数形参.*args .**kwargs 作为传递参数的函数,在使用过程中,可对*args .**kwargs多个输入,但是效率或者比较麻烦 ...
- webstorm中.vue报错(es6语法报错)-转
1.webstorm中es6语法报错,解决方法: 打开 Settings => Languages & Frameworks => Javascript把 Javascript L ...
- Vim Plugins for Linux
Usage 1.Set up Vundle: git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vi ...
- Maven 错误 Failure to transfer ...was cached in the local repository...
Maven 错误 Failure to transfer ...was cached in the local repository... 我解决的时候多了两步才解决 1. mvn clean ins ...
- python处理编码问题和JSON格式
从文件读出数据:默认utf8编码 json.dumps()输出数据:默认unicode编码 json读取(json是种通用的数据传输格式) import ujson as json #for perf ...
- CentOS7/Ubuntu Linux java jdk 环境变量配置
[root@localhost sony]# tar -zxvf jdk-8u121-linux-x64.tar.gz [root@localhost sony]# .0_121 /usr/local ...
- (转)DB2 8.2 for aix5L安装和配置步骤
DB2 8.2 for aix5L安装和配置步骤[@more@] AIX5.2上安装DB2 V8.2安装平台:IBM eserver,AIX 5.3 64位 一 删除以有的DB2系统: 1.停止DB2 ...
- Linux安装phpMyAdmin配置管理多个数据库
1.下载 :phpMyAdmin 2.解压安装包: tar -zxvf phpMyAdmin-4.6.5.2-all-languages.tar.gz 3.替换目录 : mv phpMyAdmin-4 ...