1470 Closest Common Ancestors(简单的LCA算法)

2024-03-29 06:38

本文主要是介绍1470 Closest Common Ancestors(简单的LCA算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



Closest Common Ancestors
点击打开题目链接
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 15120 Accepted: 4817

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

Hint

Huge input, scanf is recommended.

Source

下面算法出处:http://blog.csdn.net/u012860428/article/details/38306327

  • 该算法利用树中每个节点最多只有一个前驱。
  • 寻找A,B的最近祖先,假设C为A的祖先,那么沿着A一定能到C。(B也同样如此)

  • 因为是从下到上找的,所以最先找到的,就是最近的。

给出节点连接的子节点,根据此来建树,然后再给出一些数对,计算这两个节点的最近的公共节点并计数,最后全部查询完后,输出计数的个数;

[cpp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #define MAX 1000  
  5. using namespace std;  
  6. int p[MAX];  
  7. int cnt[MAX];  
  8. void init(int n)  
  9. {  
  10.     int i;  
  11.     for(i=0; i<=n; i++)  
  12.         p[i]=i;  
  13. }  
  14. int query(int x,int y)  
  15. {  
  16.     int i,j;  
  17.     if(p[x]==y)  
  18.         return y;  
  19.     if(p[y]==x)  
  20.         return x;  
  21.     for(i=x; p[i]!=i; i=p[i])//从当前节点开始,分别遍历x,y的父节点查找  
  22.     {  
  23.         for(j=y; p[j]!=j; j=p[j])  
  24.         {  
  25.             if(i==j)  
  26.             {  
  27.                 return j;  
  28.             }  
  29.         }  
  30.     }  
  31.     return i;  
  32. }  
  33. int main()  
  34. {  
  35.     int node,i,num,n,ccnode,x,y,ans,m;  
  36.     //freopen("\\input.txt","r",stdin);  
  37.    // freopen("\\output.txt","w",stdout);  
  38.     while(~scanf("%d",&n))  
  39.     {  
  40.         memset(cnt,0,sizeof(cnt));//计数数组置零  
  41.         init(n);  
  42.         m=n;  
  43.         while(n--)  
  44.         {  
  45.             scanf("\t%d\t:\t(\t%d\t)",&node,&num);  
  46.   
  47.             for(i=0; i<num; i++)  
  48.             {  
  49.                 scanf("\t%d\t",&ccnode);//输入节点  
  50.                 p[ccnode]=node;//指定节点的父亲节点  
  51.             }  
  52.         }  
  53.         scanf("%d",&n);  
  54.         for(i=0; i<n; i++)  
  55.         {  
  56.             getchar();  
  57.             scanf("\t(%d\t %d\t)",&x,&y);  
  58.             ans=query(x,y);  
  59.             cnt[ans]++;计数  
  60.         }  
  61.         //printf("%d:%d\n",14,cnt[14]);  
  62.         for(i=0; i<=m; i++)//遍历输出  
  63.         {  
  64.             if(cnt[i]!=0)  
  65.                 printf("%d:%d\n",i,cnt[i]);  
  66.         }  
  67.     }  
  68.     return 0;  

这篇关于1470 Closest Common Ancestors(简单的LCA算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/857882

相关文章

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知