pku 1330 Nearest Common Ancestors LCA离线
pku 1330 Nearest Common Ancestors
题目链接:
题目大意:
给定一棵树的边关系,注意是有向边,因为这个WA一发。然后N个顶点给出了N-1有向边,求一对点之间的最近公共祖先
思路:
裸的离线tarjan Lca即可,但注意是有向边,需要先找出根节点,数组标记。其次要注意前向星存的时候只存一条边即可
代码:
#include <iostream>
#include <string.h>
using namespace std;
const int maxn = 10005;
struct node {
int to,next;
} edges[maxn*2];
int n,head[maxn],f[maxn],vis[maxn],cnt,u,v,res,fir[maxn];
inline void addedge(int u, int v) {
edges[cnt].to=v;
edges[cnt].next=head[u];
head[u]=cnt++;
}
inline void init() {
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
memset(fir,1,sizeof(fir));
for(int i=1; i<=n; ++i) f[i]=i;
cnt=0;
}
inline int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
inline void tarjan(int s) {
vis[s]=1;
int t;
for(int i=head[s]; i!=-1; i=edges[i].next) {
t=edges[i].to;
if(!vis[t]) {
tarjan(t);
f[t]=s;
}
}
if(s==u&&vis[v]) res=Find(v);
else if(s==v&&vis[u]) res=Find(u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t,root;
cin>>t;
while(t--) {
cin>>n;
init();
for(int i=1; i<n; ++i) {
cin>>u>>v;
fir[v]=0;
addedge(u,v);
}
cin>>u>>v;
for(int i=1; i<=n; ++i) {
if(fir[i]) {
root=i;
break;
}
}
tarjan(root);
cout<<res<<endl;
}
return 0;
}
pku 1330 Nearest Common Ancestors LCA离线的更多相关文章
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
随机推荐
- input框内的单引号,双引号转译
主要是在后台传前端之前先把变量值替换单引号双引号成转译付. $bianlian是要替换的变量 两种方法 1.php后台输出值先转译 //双引号替换成转译符 $bianlian=preg_replace ...
- 阿里JAVA开发手册零度的思考理解(二)
转载请注明原创出处,谢谢! 说在前面 人生的大道上默默地走,就必须要有一盏灯亮着为你引导方向!而这盏灯抑或只是一句话,一句鼓励,一个赞美,一次承认,一次认可,一次相识一次交流-- 上篇文章:阿里JAV ...
- gops - Go语言程序查看和诊断工具
想必 Java 的开发者没有不知道或者没用过 jps 这个命令的,这个命令是用来在主机上查看有哪些 Java 程序在运行的. 我刚用 Go 语言程序的时候也很苦恼,我部署在公司服务器上的 Go 程序, ...
- A Very Simple Problem
A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 磁盘管理之 raid 文件系统 分区
第1章 RAID 磁盘阵列 1.1 使用raid的目的 1)获得更大的容量 2)让数据更安全 3)读写速度更快 1.2 raid0.raid1.raid5.raid10对比 磁头 0磁道 1扇区 前4 ...
- mac nexus搭建本地maven服务器
1.在保证安装jdk 1.7+,去nexus官网下载nexus(http://www.sonatype.com/download-oss-sonatype) 最新版本 2.解压zip文件,配置nexu ...
- JavaScript系列----AJAX机制详解以及跨域通信
1.Ajax 1.1.Ajax简介 Ajax简介这一部分我们主要是谈一下ajax的起源,ajax是什么?因为这些是跟技术无关的.所以,大多细节都是一笔带过. Ajax的起源? Ajax一词源于2005 ...
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...
- JavaScript数组对象方法
数组的方法:首先,数组的方法有数组原型方法,也有从object对象继承来的方法.这里只详细说明一下数组的原型方法. (1)join:把数组中的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. 例 ...
- 关于node的前端项目编译时内存溢出问题
最近在做一个基于vue 的多页面项目 , 页面n++多,编译时发生node内存溢出问题,继而百度之,得到解答,故记录之. '如图' 只需在 package.json 里面 加上 --max ...