POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330
Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:
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 The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.
Output Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.
Sample Input 2 Sample Output 4 Source |
这是一道裸LCA,给你一个有根树,再给你两个点判断其最近公共祖先,可以用tarjan解决
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int maxn=;
int f[maxn],n,LCA[maxn],in[maxn],vis[maxn],R;
vector<int> V[maxn];
pii P;
void init()
{
for (int i=; i<=n; i++)
V[i].clear(),f[i]=i;
memset(LCA,,sizeof(LCA));
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
}
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int mix(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx==fy) return ;
f[fx]=fy;
return ;
}
void Tarjan(int root)
{
vis[root]=;
if (P.X==root&&vis[P.Y])
{
LCA[R]=find(P.Y);
return ;//因为只有一条边,找到直接return
}
if (P.Y==root&&vis[P.X])
{
LCA[R]=find(P.X);
return ;
}
for (int i=; i<V[root].size(); i++)
{
if (!vis[V[root][i]]);
Tarjan(V[root][i]);
f[V[root][i]]=root;
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int a,b;
scanf("%d",&n);
init();
for (int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
if (a!=b)
{
in[b]++;//in记录入度
V[a].push_back(b);
}
}
scanf("%d%d",&a,&b);
P.X=a,P.Y=b;
for (int i=;i<=n;i++)
if (in[i]==)//根节点的入度为0
{
R=i;//R为根节点
Tarjan(i);
printf("%d\n",LCA[R]);
break;
}
}
return ;
}
POJ - 1330 Nearest Common Ancestors(基础LCA)的更多相关文章
- 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模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- 【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)
多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...
- POJ 1330:Nearest Common Ancestors【lca】
题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么 思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上 #include<cstdio&g ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
随机推荐
- 初次配置eclipse, jdk, tomcat, maven, mysql, alt+/
eclipse 官网下载eclipse-inst-win64.exe, 选择安装java ee. jdk 官网下载jdk-8u102-windows-x64.exe, next到底. 接下来配置环境变 ...
- php集成环境和自己配置的区别,php集成环境、php绿色集成环境、php独立安装版环境这三者的区别
最近有学生问我,直接使用PHP集成环境和我们自己独立安装的php环境有什么不一样吗? 答:PHP集成环境,和自己安装的php环境实际上没啥区别的,只不过大部分的集成环境进行了一些绿化操作,本质上没啥区 ...
- typings 命令使用注意
1.如果要查询一些库 typings search xxx 2.安装jquery node 这样的库要这样 typings dt~node --global --save 一定要dt~xxx ,然 ...
- SSH 一些错误的解决办法
1.主动访问的机器需要创建私钥和公钥 (client) #cd ~#mkdir .ssh#chmod 700 .ssh#cd .ssh#ssh-keygen -t rsa //一路回车,各种提示按默认 ...
- js遍历数组对象和非数组对象
//---------for用来遍历数组对象 var i,myArr = ["a","b","c"]; ; i < myArr.len ...
- Leetcode easy
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- Java 基础知识(一)
Java基础知识篇: 一.关键字解释 1. final:修饰非抽象类,非抽象方法和属性, 以及修饰方法参数,代表“无法改变的”.出于对设计或者效率的考虑使用该关键字. final类无法被继承,fina ...
- LoadLibraryW 参数问题
error C2664: "LoadLibraryW": 不能将参数1 从"const char [8]"转换为"LPCWSTR" 右击工程 ...
- Java动态解压zip压缩包
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; impo ...
- CSU 1810 Reverse
湖南省第十二届大学生计算机程序设计竞赛$H$题 规律,递推. 这种问题一看就有规律.可以按位统计对答案的贡献.即第$1$位对答案作出了多少贡献,第$2$位对答案作出了多少贡献.....累加和就是答案. ...