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 基于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配置邮件通知

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

基于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设