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实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则