poj 1470(LCA RE)
来源:互联网 发布:依存度算法 编辑:程序博客网 时间:2024/06/01 09:14
Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 11754 Accepted: 3875
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
LCA的第一题就RE,数组扩大之后又MLE ,跪求大牛指点啊!!RE代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define N 50010int n,m,q,e,eq;int num[N];int first[N],next[N],v[N],w[N];int first_q[N],next_q[N],u_q[N],v_q[N],lca[N];int d[N],p[N];int flag[N];bool ok[N];void init(){ e=eq=0; memset(first,-1,sizeof(first)); memset(first_q,-1,sizeof(first_q)); memset(lca,0,sizeof(lca)); memset(p,-1,sizeof(p)); memset(ok,0,sizeof(ok));}void add(int a,int b){ v[e]=b; next[e]=first[a]; first[a]=e++;}void add_q(int a,int b){ u_q[eq]=a; v_q[eq]=b; next_q[eq]=first_q[a]; first_q[a]=eq++;}void make_set(int i){ p[i]=i;}int find_set(int i){ if(i^p[i]) p[i]=find_set(p[i]); return p[i];}void union_set(int i,int j){ i=find_set(i),j=find_set(j); p[j]=i;}void dfs(int a){ int i,b; make_set(a); for(i=first[a];i!=-1;i=next[i]) { b=v[i]; if(p[b]==-1) { dfs(b); p[b]=a; } } for(i=first_q[a];i!=-1;i=next_q[i]) if(!ok[i]) { b=v_q[i]; if(p[b]!=-1)lca[i]=find_set(b),ok[i]=true; }}inline bool get(int &a){ char c; while(((c=getchar())<'0'||c>'9')&&c!=EOF); if(c==EOF)return 0; for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0'; return 1;}int main(){ int a,b,c,m,i; char s1,s2,s3; while(get(n)) { init(); memset(flag,-1,sizeof(flag)); memset(num,0,sizeof(num)); for(i=0;i<n;i++) { get(a);get(m); while(m--) { get(b); add(a,b); flag[b]++; } } get(q); while(q--) { get(a);get(b); add_q(a,b); add_q(b,a); } for(i=1;i<=n;i++) if(flag[i]==-1) { dfs(i); } for(int i=0;i<eq;i+=2) { a=u_q[i]; b=v_q[i]; c=lca[i]; if(!c) c=lca[i+1]; num[c]++; } for(i=1;i<=n;i++) if(num[i])printf("%d:%d\n",i,num[i]/2); } return 0;}
- poj 1470(LCA RE)
- POJ 1470Closest Common Ancestors(LCA)
- poj 1470--tarjan--LCA
- POJ 1470 LCA
- poj 1470(LCA)
- poj 1330(LCA)
- poj 1470(LCA模板题)
- POJ 1470 LCA 公共祖先
- poj 1470 LCA 离线算法
- poj 1470 LCA基础题
- poj 1470 LCA倍增 裸
- poj 1470 Closest Common Ancestors(tarjan离线求lca)
- Poj 1470 Closest Common Ancestors(Tarjin 求LCA)
- POJ 1470 Closest Common Ancestors (离线LCA,4级)
- poj 1470 Closest Common Ancestors(简单的LCA算法)
- POJ 1470 Closest Common Ancestors(LCA入门题)
- POJ 题目1470 Closest Common Ancestors(LCA)
- poj 1470 Closest Common Ancestors (离线LCA Tarjan)
- 非静态的字段、方法或属性要求对象引用
- 完美解决窃取ssh private key问题
- 好的编程学习资源
- 五分钟讲清楚UML
- 最近练习的一些算法(持续更新)
- poj 1470(LCA RE)
- until cancel的意义和使用场景
- hdu1828poj1177线段树周长并
- Flex服务器swf 更新后,客户端不需要清空缓存即可查看最新版本
- POJ3725-I know the k-th integer
- K&R的名著:<C程序设计语言>小程序总结2
- Ubuntu下配置 Eclipse 编译、开发 Hadoop(MapReduce)源代码
- Android多媒体开发【6】-- 播放器的基本模型
- dwarf调试信息格式入门