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 }
- 数据在内存中的存储
- 数据抓取与利用行为的不正当竞争法规制
- python学习手册-爬爬那些年咱没见过的校花图片
- 正则表达式游戏的答案
- .NET Core的日志[2]:将日志输出到控制台
- 2017年高等教育十大战略性技术(二)
- 通过实例模拟ASP.NET MVC的Model绑定机制:数组
- 《全球贸易信息动态》
- .NET Core的日志[3]:将日志写入Debug窗口
- Code2Cloud:比ALM中断更大
- .NET Core的日志[4]:将日志写入EventLog
- 微信小程序不行了?看小马哥带你忆童年
- ASP.NET MVC三个重要的描述对象:ControllerDescriptor和ActionDescriptor的创建
- .NET Core的日志[5]:利用TraceSource写日志
- 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 数组属性和方法
- 解密 Docker 挂载文件,宿主机修改后容器里文件没有修改
- SAP Spartacus的单元测试目录结构
- 突击并发编程JUC系列-并发工具 CyclicBarrier
- 详解 | Linux系统是如何实现存储并读写文件的?
- Angular单元测试的一个错误消息
- Python进行特征提取
- dotnet OpenXML 元素 cNvPr NonVisual Drawing Properties 的属性作用
- Java 对象相关面试题
- 【每日一题】【vue2源码学习】VUE中模版编译原理
- 【每日一题】【vue2源码学习】对VUE响应式数据的理解
- ApacheCN 深度学习译文集 2020.9
- 当Docker遇到Intellij IDEA,再次解放了生产力~
- 基于NPOI的Excel导入导出类库
- 在tinycolinux32上装tinycolinux64 kernel和toolchain
- 通过链下签名授权实现更少 Gas 的 ERC20代币