poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors
Time Limit: 2000MS |
Memory Limit: 10000K |
|
---|---|---|
Total Submissions: 15446 |
Accepted: 4944 |
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
给你一棵树,要你找出一些节点的最近公共祖先
代码:
1 /*poj 1470*/
2 #include<iostream>
3 #include<vector>
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 using namespace std;
8 const int maxn=902;
9 vector<int> tree[maxn],qus[maxn];
10 int rank[maxn],father[maxn];
11 bool vis[maxn];
12 int rudu[maxn];
13 int lroot[maxn];
14 int ans[maxn];
15
16 void init(int n){
17 memset(vis,0,sizeof(char)*(n+1));
18 memset(rudu,0,sizeof(int)*(n+1));
19 memset(lroot,0,sizeof(int)*(n+1));
20 memset(ans,0,sizeof(int)*(n+1));
21 for(int i=1;i<=n;i++){
22 father[i]=i;
23 rank[i]=1;
24 tree[i].clear();
25 qus[i].clear();
26 }
27 }
28
29 int find(int a){
30 while(a!=father[a])
31 a=father[a];
32 return a;
33 }
34
35 void Union(int a,int b)
36 {
37 int x=find(a);
38 int y=find(b);
39 if(x==y) return ;
40 if(rank[x]<rank[y]){
41 rank[y]+=rank[x];
42 father[x]=y;
43 }
44 else {
45 rank[x]+=rank[y];
46 father[y]=x;
47 }
48 }
49
50 void LCA(int u)
51 {
52 lroot[u]=u;
53 //vis[u]=1; 不能放在这里
54 int len=tree[u].size();
55 for(int i=0;i<len;i++){
56 LCA(tree[u][i]);
57 Union(u,tree[u][i]);
58 lroot[find(u)]=u;
59 }
60 vis[u]=1;
61 int ss=qus[u].size();
62 for(int i=0;i<ss;i++){
63 if(vis[qus[u][i]]){
64 ans[lroot[find(qus[u][i])]]++;
65 //return ;
66 }
67 }
68 }
69
70 int main()
71 {
72 int n,m,t,u1,u2;
73 freopen("test.in","r",stdin);
74 while(scanf("%d",&n)!=EOF){
75 init(n);
76 for(int i=0;i<n;i++){
77 getchar();
78 scanf("%d:(%d))",&u1,&m);
79 for(int j=0;j<m;j++){
80 scanf("%d",&u2);
81 tree[u1].push_back(u2);
82 rudu[u2]++;
83 }
84 }
85 scanf("%d",&t);
86 for(int i=0;i<t;i++)
87 {
88 scanf("%*1s%d%d%*1s",&u1,&u2);
89 qus[u1].push_back(u2);
90 qus[u2].push_back(u1);
91 }
92 for(int i=1;i<=n;i++)
93 {
94 if(rudu[i]==0)
95 {
96 LCA(i);
97 break;
98 }
99 }
100 for(int i=1;i<=n;i++){
101 if(0!=ans[i])
102 printf("%d:%dn",i,ans[i]);
103 }
104 }
105 return 0;
106 }
- 如何使用HAProxy实现Kerberos环境下的Impala负载均衡
- Hue中使用Oozie创建Shell工作流在脚本中切换不同用户
- Python学习(七):模块 优雅的封装
- 非Kerberos环境下Kafka数据到Flume进Hive表
- 6.如何为Hue配置OpenLDAP认证
- Socket 通信原理
- PHP 面试知识梳理
- 5.如何为Impala配置OpenLDAP认证
- 传统Spring项目使用FeignClient组件访问微服务
- 4. 如何为Hive配置OpenLDAP认证
- 3.如何实现OpenLDAP的主主同步
- 2.OpenLDAP集成SSH登录并使用SSSD同步用户
- 1.如何在CentOS6.5安装OpenLDAP并配置客户端
- PHP 面试知识梳理
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android Studio升级到3.0后遇到的坑
- Android原生项目集成React Native的方法
- Android编程之光线传感器用法详解
- Android Studio 3.0 新功能全面解析和旧项目适配问题
- Android开发中使用外部应用获取SD卡状态的方法
- Android编程使用光线传感器获取光线强弱的方法【LightSensorManager封装类】
- Android开发中的重力传感器用法实例详解
- 腾讯云TKE-Ingress案例: Nginx-Ingress 实现grpc转发
- 机器人软件中间层 yarp-Yet Another Robot Platforms
- 3分钟短文:Laravel写个命令行,你就是下一个Geek!
- Android开发多年每天Crud不清楚自己的技术?来刷刷大厂的高端技术面试题就知道了
- MySQL案例:count(*)效率优化
- MUI进行APP混合开发实现下拉刷新和上拉加载 原创
- Android 给控件添加边框阴影效果
- 详解Android Selinux 权限及问题