POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form:

nr_of_vertices

vertex:(nr_of_successors) successor1 successor2 ... successorn

...

where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form:

nr_of_pairs

(u v) (x y) ...

The input file contents several data sets (at least one).

Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times

For example, for the following tree:

Sample Input

5

5:(3) 1 4 2

1:(0)

4:(0)

2:(1) 3

3:(0)

6

(1 5) (1 4) (4 2)

(2 3)

(1 3) (4 3)

Sample Output

2:1

5:5

Http

POJ:https://vjudge.net/problem/POJ-1470

Source

最近公共祖先LCA

题目大意

给出一棵树,统计若干组对最近公共祖先的询问,输出每个点被统计为最近公共祖先多少次

解决思路

这个题就是多次统计LCA,笔者在这里采用在线倍增的方法,具体操作可以看笔者之前的文章

这个题最恶心的地方就是输入了

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; const int maxN=901;
const int inf=2147483647; int n;
int root;
vector<int> E[maxN];
int Parent[maxN][20];
int Depth[maxN];
int Cnt[maxN];
bool vis[maxN]; void LCA_init();
void dfs(int u);
int LCA(int a,int b); int main()
{
while (cin>>n)
{
for (int i=1;i<=n;i++)
E[i].clear();
memset(Parent,0,sizeof(Parent));
memset(Depth,0,sizeof(Depth));
memset(Cnt,0,sizeof(Cnt));
memset(vis,0,sizeof(vis));
for (int i=1;i<=n;i++)//-------输入开始-------
{
int u,nn;
scanf("%d:(%d)",&u,&nn);
for (int j=1;j<=nn;j++)
{
int v;
scanf("%d",&v);
E[u].push_back(v);
vis[v]=1;
}
}
for (int i=1;i<=n;i++)
if (vis[i]==0)
{
root=i;
break;
}
LCA_init();
int Q;
scanf("%d",&Q);
for (int i=1;i<=Q;i++)
{
int u,v;
scanf(" (%d %d)",&u,&v);
//cout<<LCA(u,v)<<endl;
Cnt[LCA(u,v)]++;
}//-------输入结束-------
for (int i=1;i<=n;i++)
if (Cnt[i]!=0)
printf("%d:%d\n",i,Cnt[i]);
}
return 0;
} void LCA_init()//LCA初始化
{
Depth[root]=0;
dfs(root);
/*for (int i=1;i<=n;i++)
{
for (int j=0;j<=15;j++)
cout<<Parent[i][j]<<' ';
cout<<endl;
}
cout<<endl;*/
for (int j=1;j<=15;j++)
for (int i=1;i<=n;i++)
Parent[i][j]=Parent[Parent[i][j-1]][j-1];
/*for (int i=1;i<=n;i++)
{
for (int j=0;j<=15;j++)
cout<<Parent[i][j]<<' ';
cout<<endl;
}*/
return;
} void dfs(int u)
{
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i];
Depth[v]=Depth[u]+1;
Parent[v][0]=u;
//cout<<"---"<<v<<' '<<Parent[v][0]<<endl;
dfs(v);
}
return;
} int LCA(int a,int b)//倍增法计算LCA
{
if (Depth[a]<Depth[b])
swap(a,b);
for (int i=15;i>=0;i--)
if ((Parent[a][i]!=0)&&(Depth[Parent[a][i]]>=Depth[b]))
a=Parent[a][i];
if (a==b)
return a;
for (int i=15;i>=0;i--)
if ((Parent[a][i]!=0)&&(Parent[b][i]!=0)&&(Parent[a][i]!=Parent[b][i]))
{
a=Parent[a][i];
b=Parent[b][i];
}
return Parent[a][0];
}

POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)的更多相关文章

  1. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  2. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  3. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  4. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  5. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  6. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  7. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

  8. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  9. poj 1470 Closest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...

随机推荐

  1. oracle创建用户四部曲

    创建用户一般分四步: 第一步:创建临时表空间 第二步:创建数据表空间 第三步:创建用户并制定表空间 第四步:给用户授予权限 创建临时表空间 create temporary tablespace ho ...

  2. 【 js 基础 】【 源码学习 】源码设计 (持续更新)

    学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析第二部分:undersc ...

  3. C#实现SQLSERVER数据库中有序GUID生成(NewSequentialId)

    GUID作为数据库主键由于其无序性所以性能不怎么好,SQL Server中有个函数NewSequentialId可以生成有序的GUID,由于在程序中需要用到,就用C#实现了一下,生成的GUID格式基本 ...

  4. HTML----网页基础和基本标签

    网页分类: 1.静态网页:所有内容全写死,都写在源代码中,若修改必须修改源代码,后缀为.html或htm 2.动态网页:内容大部分来自于数据库,可以修改,后缀为.aspx(c#).jsp(java). ...

  5. Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建

    http://blog.csdn.net/u013142781/article/details/50380920

  6. java加密算法入门(二)-对称加密详解

    1.简单介绍 什么是对称加密算法? 对称加密算法即,加密和解密使用相同密钥的算法. 优缺点: 优点:算法公开.计算量小.加密速度快.加密效率高. 缺点: (1)交易双方都使用同样钥匙,安全性得不到保证 ...

  7. C++构造函数(一)

    本篇是介绍C++的构造函数的第一篇(共二篇),属于读书笔记,对C++进行一个系统的复习. 构造函数的概念和作用 全局变量未初始化时为0,局部变量未初始化时的值却是无法预测的.这是因为,全局变量的初始化 ...

  8. Go - method

    hello, 大家好,由于之前工作上面的事情较多,所以关于go语言的学习就暂时“搁浅了”...不过从今天开始,我们又将回到了go语言的学习过程之中. 当然,我们学习go的"初心"是 ...

  9. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  10. [平衡树] mingap

    时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 9 题目描述 实现一种数据结构,维护以下两个操作: (1) I x :加入元素 x : (2) M :输出当前表中相差最小的两 ...