博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ1470】Closest Common Ancestors
阅读量:5115 次
发布时间:2019-06-13

本文共 3151 字,大约阅读时间需要 10 分钟。

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

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
 

【题意】

  求LCA。

【分析】

  跟上一题差不多,注意输入,没有那么复杂的。

 

代码如下:

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 #define Maxn 10010 8 #define INF 100000000 9 10 int fa[Maxn],first[Maxn],size[Maxn],dep[Maxn],son[Maxn]; 11 int w[Maxn],top[Maxn];int wl; 12 bool q[Maxn]; 13 int sum[Maxn]; 14 15 struct node 16 { 17 int x,y,next; 18 }t[2*Maxn];int len; 19 20 int mymax(int x,int y) {
return x>y?x:y;} 21 int mymin(int x,int y) {
return x
size[son[x]]) son[x]=t[i].y; 38 } 39 } 40 41 void dfs2(int x,int tp) 42 { 43 w[x]=++wl; 44 top[x]=tp; 45 if(size[x]!=1) dfs2(son[x],tp); 46 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa[x]&&t[i].y!=son[x]) 47 { 48 dfs2(t[i].y,t[i].y); 49 } 50 } 51 52 int LCA(int a, int b) 53 { 54 while (1) 55 { 56 if(top[a]==top[b]) return dep[a]<=dep[b]?a:b; 57 else if(dep[top[a]]>=dep[top[b]]) a=fa[top[a]]; 58 else b=fa[top[b]]; 59 } 60 } 61 62 63 64 int main() 65 { 66 int n; 67 while(scanf("%d",&n)!=EOF) 68 { 69 memset(first,0,sizeof(first)); 70 memset(q,0,sizeof(q)); 71 len=0; 72 for(int i=1;i<=n;i++) 73 { 74 int x,y,z; 75 scanf("%d:(%d) ",&x,&y); 76 while(y--) 77 { 78 scanf("%d",&z); 79 ins(x,z);q[z]=1; 80 } 81 } 82 int root; 83 for(int i=1;i<=n;i++) if(!q[i]) root=i; 84 dep[0]=0;size[0]=0; 85 dfs1(root,0);wl=0; 86 dfs2(root,root); 87 int m; 88 scanf("%d",&m);getchar(); 89 memset(sum,0,sizeof(sum)); 90 for(int i=1;i<=m;i++) 91 { 92 int x,y; 93 scanf(" (%d %d)",&x,&y); 94 sum[LCA(x,y)]++; 95 } 96 for(int i=1;i<=n;i++) if(sum[i]!=0) 97 printf("%d:%d\n",i,sum[i]); 98 } 99 return 0;100 }
[POJ1470]
 

 

2016-05-10 13:14:31

 
 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5477465.html

你可能感兴趣的文章
rsync
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
查看>>
图片等比例缩放及图片上下剧中
查看>>
WebView加载网页详情
查看>>
【转载】Linux screen 命令详解
查看>>
dd命令 建立两颗一模一样的磁盘
查看>>
常用的jquery触屏手机页面特效代码下载
查看>>
background-clip,background-origin
查看>>
C# 如何创建一个Windows服务
查看>>
集群和分布式区别
查看>>
Android(java)学习笔记153:采用post请求提交数据到服务器(qq登录案例)
查看>>
Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
查看>>
Android 高级UI设计笔记12:ImageSwitcher图片切换器
查看>>
虚拟主机与虚拟目录学习小结
查看>>
hlg1414安装雷达【贪心】
查看>>