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

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj1330(LCA最近公共祖先)

题意:求最近公共祖先 思路:之前学习了树链剖分,然后我就用树链剖分的一小部分知识就可以解这个题目了,记录每个结点的fa和depth。然后查找时,每次将depth大的结点往上走直到x = y。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直