poj1470(Closest Common Ancestors)
来源:互联网 发布:幸运28预测软件 编辑:程序博客网 时间:2024/05/21 04:42
Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 12677 Accepted: 4140
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.
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:
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.
Source
Southeastern Europe 2000
include<iostream>//244k,579ms#include<vector>using namespace std;const int N=905;vector<int>a[N];//,b(N);int f[N],r[N],b[N];void dfs(int u,int deep){r[u]=deep;for(vector<int>::iterator it=a[u].begin();it!=a[u].end();++it)dfs(*it,deep+1);}int main(){//freopen("8.4.9.input.txt","r",stdin);int n,i,j,x,y,t;while(scanf("%d",&n)==1){memset(f,-1,sizeof(f));memset(r,-1,sizeof(r));for(i=0;i<n;i++) a[i].clear();memset(b,0,sizeof(b));for(i=0;i<n;i++){scanf("%d:(%d)",&x,&t);for(j=0;j<t;j++){scanf("%d",&y);a[x-1].push_back(y-1);f[y-1]=x-1;}}for(i=0;f[i]>=0;i++);dfs(i,0);scanf("%d",&t);for(i=0;i<t;i++){ while(getchar()!='(');scanf("%d%d",&x,&y);while(getchar()!=')');x--;y--;while(x!=y){if(r[x]>r[y]) x=f[x];else y=f[y];}x++;b[x]++;}for(i=1;i<=n;i++){if(b[i]!=0)printf("%d:%d\n",i,b[i]);}}return 0;}
- POJ1470--Closest Common Ancestors
- poj1470 Closest Common Ancestors
- poj1470 Closest Common Ancestors
- POJ1470 Closest Common Ancestors LCA
- poj1470-Closest Common Ancestors(LCA)
- poj1470——Closest Common Ancestors//LCA
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
- poj1470 Closest Common Ancestors 离线LCA
- Closest Common Ancestors【POJ1470】——LCA
- POJ1470/ZOJ1141 Closest Common Ancestors(LCA离线算法)
- poj1470 Closest Common Ancestors LCA(最近公共祖先)
- 【LCA最近公共祖先】 poj1470 Closest Common Ancestors
- poj1470 Closest Common Ancestors(LCA离线优化)
- poj1470 Closest Common Ancestors Lca离线算法Tarjan
- poj1470(Closest Common Ancestors)
- Closest Common Ancestors
- G - Closest Common Ancestors
- Pku 1470 Closest Common Ancestors
- [2013.4.18]java+Eclipse环境下ArcEngine10 开发环境搭建
- IOs开发Objective-C基础之──多态
- Frame-Relay基础及配置学习笔记
- 今天的收获 2013.4.18
- Android开发视频教学第一季(1
- poj1470(Closest Common Ancestors)
- jQuery学习(四) 效果——隐藏/显示
- JS 随机更换图片
- Keil 【警告+错误】提示汇总
- 一致性哈希算法
- Jquery div 中获得元素
- C#中值类型和引用类型的区别
- java serialization--java序列化
- LCD 驱动调试log