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 ...
随机推荐
- C++函数返回值(02)
对象作为返回值 编译器会将函数栈中的返回值数据拷贝到返回栈中 指针作为返回值 函数的返回值可以是存储某种类型数据的内存地址,称这种函数为指针函数.它们的一般定义形式如下: 类型标识符 *函数名(参数 ...
- jstl 中 <c:foreach> 多级循环
- 使用Xmanager通过XDMCP连接远程Centos 7 (摘自xmanager官方博客)
Using Xmanager to connect to remote CentOS 7 via XDMCP Gnome in CentOS 7 tries to use local hardware ...
- OpenCV4Android背景建模(MOG、MOG2)
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 很久以前的笔记了,分享给大家吧...OpenCV4Android中用于背景建模的类主要 ...
- 五、在IDEA中使用GIt版本控制并将本地代码上传至Github
一.安装git Git工具下载:https://git-scm.com/downloads 从官网下载.安装很简单,基本都是下一步. 安装完的第一件事情就是创建一个Git用户: git con ...
- IsoAlgo3d - A PCF 3D Viewer for Desktop, Tablet and Smart phone
IsoAlgo3d - A PCF 3D Viewer for Desktop, Tablet and Smart phone eryar@163.com Abstract. IsoAlgo3d 通过 ...
- How Many Answers Are Wrong
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- JAVANIO通道
package com.nio.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- 分酒问题(DFS解法)
题目大概是这样: 已知有三个容量分别为3千克.5千克和8千克的并且是没有刻度的酒瓶,3千克和5千克的瓶子均装满了酒,而8千克的瓶子为空.现要求仅用这三个酒瓶将这些酒均分为两个4千克并分别装入5千克和8 ...
- css中单位 px、em 的区别【转载】
原文:http://www.admin10000.com/document/6267.html 在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了p ...