POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330
Nearest Common Ancestors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 34942 | Accepted: 17695 |
Description
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
Input
Output
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
题意概括:
给一棵有N个节点,N-1条边的树 和 一对结点,求这对结点的最近公共祖先。
解题思路:
找根结点用一个标记数组
找公共祖先用简单粗暴的 Tarjan。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e4+;
struct Edge{int v, next;}edge[MAXN<<];
int head[MAXN], cnt;
int fa[MAXN];
bool in[MAXN];
bool vis[MAXN];
int N, M, S, ans, a, b; inline void init()
{
memset(head, -, sizeof(head));
memset(vis, false, sizeof(vis));
memset(in, false, sizeof(in));
cnt = ;
} inline void AddEdge(int from, int to)
{
edge[cnt].v = to;
edge[cnt].next = head[from];
head[from] = cnt++;
} int findset(int x)
{
int root = x;
while(fa[root] != root) root = fa[root]; int tmp;
while(fa[x] != root){
tmp = fa[x];
fa[x] = root;
x = tmp;
}
return root;
} void Tarjan(int s)
{
fa[s] = s;
for(int i = head[s]; i != -; i = edge[i].next){
int Eiv = edge[i].v;
Tarjan(Eiv);
fa[findset(Eiv)] = s;
}
vis[s] = true;
if(s == a){
if(vis[a] && vis[b]) ans = findset(b);
}
else if(s == b){
if(vis[a] && vis[b]) ans = findset(a);
}
} int main()
{
int T_case, u, v;
scanf("%d", &T_case);
while(T_case--)
{
init();
scanf("%d", &N);
M = N-;
for(int i = ; i <= M; i++){
scanf("%d %d", &u, &v);
AddEdge(u, v);
in[v] = true;
//AddEdge(v, u);
}
scanf("%d %d", &a, &b);
int root = ;
for(int i = ; i <= N; i++){
if(!in[i]){root = i;break;}
}
Tarjan(root);
printf("%d\n", ans);
}
return ;
}
POJ 1330 Nearest Common Ancestors 【LCA模板题】的更多相关文章
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- 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 LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- 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 A rooted tree is a well-known data structure in computer science a ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
随机推荐
- C#中if和#if区别
if的作用是程序流控制,会直接编译.执行.#if是对编译器的指令,其作用是告诉编译器,有些语句行希望在条件满足时才编译. --------------------------------------- ...
- 深入redis内部之redis启动过程之一
redis作为一个服务器,它的启动是从main函数开始的.redis.c 1. 进程重命名 #ifdef INIT_SETPROCTITLE_REPLACEMENT spt_init(argc, ar ...
- Linux VFS机制简析(二)
Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...
- 深入理解JavaScript系列(30):设计模式之外观模式
介绍 外观模式(Facade)为子系统中的一组接口提供了一个一致的界面,此模块定义了一个高层接口,这个接口值得这一子系统更加容易使用. 正文 外观模式不仅简化类中的接口,而且对接口与调用者也进行了解耦 ...
- C# 获取字符串长度
int leng = System.Text.Encoding.Default.GetBytes(attachfileId2).Length;
- 一、IP地址
IP地址 1)网络地址 IP地址由网络号(包括子网号)和主机号组成,网络地址的主机号为全0,网络地址代表着整个网络. 2)广播地址 广播地址通常称为直接广播地址,是为了区分受限广播地址. 广播地址与网 ...
- 解决flashfxp连接虚拟机报错 530 permission denied
菜鸟使用flashfxp遇到连接报错. [21:36:19] [R] 530 Permission denied.[21:36:19] [R] 连接失败 (连接已被客户端关闭) 搜索后发现,是因为li ...
- Android开发之EditText利用键盘跳转到下一个输入框
以前做项目的时候,从来没考虑过这些.这段时间公司内部用的一款APP,就出现了这个问题,在登录或者注册的时候,点击键盘的回车按钮,可以跳到下一个输入框的功能,这个属性一直也没记住,今天就把自己一直没记过 ...
- 16_AOP入门准备_Jdk动态代理模式
[工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...
- 学习JVM虚拟机原理总结
0x00:JAVA虚拟机的前世今生 1991年,在Sun公司工作期间,詹姆斯·高斯林和一群技术人员创建了一个名为Oak的项目,旨在开发运行于虚拟机的编程语言,允许程序多平台上运行.后来,这项工作就演变 ...