poj 1470 Closest Common Ancestors(LCA)
来源:互联网 发布:java的equals方法 编辑:程序博客网 时间:2024/05/20 09:22
题目链接:http://poj.org/problem?id=1470
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
输入很麻烦,看了讨论发现了处理技巧,用%1s。。不过时间和大家比起来还是差很多,还是太菜了...
代码:
输入很麻烦,看了讨论发现了处理技巧,用%1s。。不过时间和大家比起来还是差很多,还是太菜了...
代码:
#include<stdio.h>#include<string.h>const int maxn=250000;struct node{ int from,to,next;}e[maxn];int head[maxn],vis[maxn],p[maxn];int x[maxn],y[maxn];int ans[maxn];int n,m;int cnt;void add(int x,int y){ e[cnt].from=x; e[cnt].to=y; e[cnt].next=head[x]; head[x]=cnt++;}int find(int x){ if(p[x]!=x) p[x]=find(p[x]); return p[x];}void LCA(int u){ p[u]=u; for(int k=head[u];k!=-1;k=e[k].next) { if(!vis[e[k].to]) { LCA(e[k].to); p[e[k].to]=u; } } vis[u]=1; for(int i=1;i<=m;i++) { if(x[i]==u&&vis[y[i]]) ans[find(y[i])]++; if(y[i]==u&&vis[x[i]]) ans[find(x[i])]++; }}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d",&n)==1) { cnt=0; memset(ans,0,sizeof(ans)); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(p,-1,sizeof(p)); char ch1[3],ch2[3],ch3[3]; int id,t; for(int i=1;i<=n;i++) { scanf("%d%1s%1s%d%1s",&id,ch1,ch2,&t,ch3); while(t--) { int son; scanf("%d",&son); add(id,son); vis[son]=1; } } scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%1s%d%d%1s",ch1,&x[i],&y[i],ch2); int j; for(int i=1;i<=n;i++) if(!vis[i]) { j=i; break; } memset(vis,0,sizeof(vis)); LCA(j); for(int i=1;i<=n;i++) if(ans[i]) printf("%d:%d\n",i,ans[i]); } return 0;}
0 0
- POJ-1470 Closest Common Ancestors【LCA】
- POJ-1470 Closest Common Ancestors【LCA】
- POJ 1470 Closest Common Ancestors [LCA+RMQ]
- poj 1470 Closest Common Ancestors(LCA)
- poj--1470--Closest Common Ancestors--LCA
- POJ 1470 Closest Common Ancestors LCA题解
- Closest Common Ancestors - POJ 1470 LCA
- poj 1470 Closest Common Ancestors(LCA)
- 【LCA】 POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors(LCA)
- POJ - 1470 Closest Common Ancestors(LCA离线)
- POJ 1470Closest Common Ancestors(LCA)
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- [POJ 1470]Closest Common Ancestors[离线LCA]
- poj 1470 Closest Common Ancestors LCA离线
- POJ 1470 Closest Common Ancestors (LCA)
- 【LCA基础】poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors 离线LCA
- 2进制与8,16,10进制的相互转换(带小数点)
- 随笔:关于AT命令
- 收藏学习各位大大的Blog文章
- 关于Eclipse 中用Ant编译Java程序的JDK环境设置问题
- 论语新解
- poj 1470 Closest Common Ancestors(LCA)
- AOP 你想干什么 IOC 你服务什么
- 博客导航
- 设置Spinner弹出框/下拉框的样式--最简单有效的方式
- Binary Tree Level Order Traversal II
- Can ORACLE use both memory_target and AIX large pages at the same time ?
- 如何实现赋值运算符函数
- 用代理测试ArrayList和LinkedList的添加和删除元素所用的时间
- 栈和队列