POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程
来源:互联网 发布:淘宝卖家工具131458 编辑:程序博客网 时间:2024/06/03 17:20
A - Closest Common Ancestors(8.4.9)
Time Limit:2000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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 descri_ption, 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 descri_ption 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.
nr_of_vertices
vertex:(nr_of_successors) successor1 successor2 ... successorn
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree descri_ption 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:
For example, for the following tree:
Sample Input
55:(3) 1 4 21:(0)4:(0)2:(1) 33:(0)6(1 5) (1 4) (4 2) (2 3)(1 3) (4 3)
Sample Output
2:15:5
Hint
Huge input, scanf is recommended.
accode
#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>#define maxn 900+5using namespace std;vector<int>tree[maxn];int q[maxn][maxn],flag[maxn];int ans[maxn],set[maxn],n;int set_find(int x){ if(set[x]<0)return x; return set[x]=set_find(set[x]);}void tarjan(int x){ int i; for(i=0;i<tree[x].size();++i){ tarjan(tree[x][i]); set[tree[x][i]]=x; } flag[x]=true; for(i=1;i<=n;++i){ if(flag[i]&&q[x][i]) ans[set_find(i)]+=q[x][i]; }}int main(){ int t,num,x,y,i; while(~scanf("%d",&n)){ for(i=1; i<=n; i++) tree[i].clear(); memset(flag,0,sizeof(flag)); memset(q,0,sizeof(q)); memset(ans,0,sizeof(ans)); memset(set,0,sizeof(set)); for(i=0;i<n;++i){ scanf("%d:(%d)",&t,&num); while(num--){ scanf("%d",&x); tree[t].push_back(x); set[x]=-1; } } scanf("%d",&num); for(i=0;i<num;++i){ scanf(" (%d %d)",&x,&y); q[x][y]++; q[y][x]++; } for(i=1;i<=n;++i){ if(set[i]==0){ set[i]=-1; tarjan(i); break; } } for(i=1;i<=n;++i) if(ans[i]) printf("%d:%d\n",i,ans[i]); } return 0;}
0 0
- POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程
- POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- poj - 1470 - Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- POJ---1470- Closest Common Ancestors
- POJ 1470Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors
- POJ 1470Closest Common Ancestors
- poj 1470 Closest Common Ancestors(简单的LCA算法)
- C/C++常用库及工具
- 将多个UIImage合并成一个UIImage
- JAVA常见面试题
- Adapter
- 【树状数组】POJ 3321 Apple Tree
- POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程
- 简单的同步
- 常见的机器学习与数据挖掘知识点之常见分布
- 【LeetCode】(233)Number of Digit One (Medium)
- Android学习0825<七>(ListView和Adapter拓展)
- Android下SQLite数据库学习笔记5——Android下数据库的事务
- 运用c:foreach循环显示
- DevOps:怎么实现源代码注释和系统文档的自动化更新?
- Android之ListView原理学习与优化总结