POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
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)的更多相关文章
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- 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 ...
随机推荐
- 跟着刚哥深入学maven
前言:目前所有的项目都在使用maven,可是一直没有时间去整理学习,这两天正好有时间,好好的整理一下. 一.为什么使用Maven这样的构建工具[why] ① 一个项目就是一个工程 如果项目非常庞大,就 ...
- linux中的重要目录
1./boot 引导程序,内核的存放的目录. 此目录,包含了在引导过程中所必须的文件,引导程序的相关文件(如:grub,lilo以及相应的配置文件及linux操作系统内核相关文件). 2./sbin/ ...
- Python3.6_x86通过FFpmeg获取视频或音频的时长和分辨率
前言 前段时间公司在做流媒体服务,与许多厂家合作拿了许多视频过来,现在要对这些视频文件进行整理,通过特殊的编码排列,获取他们的时长以及分辨率,这里我遇到一个大坑,请往下面看. # -*- coding ...
- [bzoj4872]分手是祝愿
Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态 ...
- 什么是Web Worker?
简单点说,Web Worker就是一个运行在后台的JavaScript线程,不会影响页面的响应. 我们知道,JavaScript是单线程的脚本语言,即同一时刻只能做一件事情,否则会带来极其复杂的同步问 ...
- jsp 文件使用 include指令 导入 jspf 分析,及导入jspf 文件后出现乱码问题
1.为什么要导入jspf文件 在做网站开发中,因为有很多的页面的导航栏是相同的,所以我们要把导航栏提取出来,生成一个jspf文件. 然后在jsp页面中使用 include 指令 导入jspf文件,这样 ...
- 使用HttpClient 调用Web Api
C#4.5 添加了异步调用Web Api . 如果你的项目是4.5以上版本,可以直接参考官方文档. http://www.asp.net/web-api/overview/web-api-client ...
- [译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口)
应用不只是包含textboxs和labels,还包含actions,如按钮和鼠标事件等.接下来我们加上一些像按钮这样的UI元素来看MVVM类怎么演变的.与之前的UI相比,这次我们加上一个"C ...
- Sql Server日期时间格式转换
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE() ...
- 记一次linux主机名莫名其妙变成了bogon
起因:公司网络接口做了接口认证,虚拟机桥接至物理网卡无法完成认证进行网络访问,无奈之下只能讲虚拟机网络模式更改为NAT模式,更改完成之后进行ssh登录,发现主机名发生了变化. 更改NAT模式之前 [r ...