注意:poj上的数据与zoj不同,第二处输入没有逗号 ' , '

题意:输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数。

思路:直接求,两个节点一直往上爬,知道爬到同一个节点,这个节点即为最近公共祖先。

这道题的输入挺······,空格可以随意输入。Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

5  :  (  3  )

scanf("%d%1s%1s%d%1s",&fa,s1,s2,&m,s3);    //第一处输入,      s1为":"      s2为"("      s3为")"。

(2,3)
scanf("%1s%d%1s%d%1s",s1,&a,s2,&b,s3);    //第二处输入,       s1为"("      s2为","      s3为")"。

%1s读取一个字符

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std; const int N=1000;
int f[N];//
int sum[N];//记录次数 int depth(int x){//计算层数
int dep=0;
while(x!=-1){
x=f[x];
dep++;
}
return dep;
}
int main(){
char s1[10],s2[10],s3[10];
int n;
char c;
while(scanf("%d",&n)!=EOF){//树的节点个数
memset(f,-1,sizeof(f));
memset(sum,0,sizeof(sum));
int tempn=n;
while(n--){
int fa,so;
int m;
scanf("%d%1s%1s%d%1s",&fa,s1,s2,&m,s3); //注意输入!!!
while(m--){
scanf("%d",&so);
f[so-1]=fa-1;
}
}
int i;
for(i=0;f[i]>=0;i++);
int n2;
scanf("%d",&n2);//要查询的最近公共祖先的节点对数
while(n2--){
int a,b;
scanf("%1s%d%1s%d%1s",s1,&a,s2,&b,s3);//注意输入!!!
a--;b--;
int depa=depth(a);
int depb=depth(b);
while(a!=b){
if(depa>depb)a=f[a],depa--;
else b=f[b],depb--;
}
sum[a]++;
}
for(i=0;i<tempn;i++){
if(sum[i]>0)
printf("%d:%d\n",i+1,sum[i]);
}
}
return 0;
}

ZOJ 1141 Closest Common Ancestors(LCA)的更多相关文章

  1. ZOJ 1141:Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that tak ...

  2. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  3. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  4. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  5. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  6. 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换

    [简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...

  7. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  8. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  9. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

随机推荐

  1. JVM内存最大能调多大分析【经典】

       http://hi.baidu.com/suofang/blog/item/49c637c71c0afbd0d1006028.html  上次用weblogic 把 -XmxXXXX 设成2G, ...

  2. Easy UI form表单提交 IE浏览器不执行success ,以及 datagrid 展示过慢

    最近在做一个Easy ui的项目 发现了一些问题,在这里总结下 1.表单提交,后端代码 public ActionResult Save(Request model) { ResultInfo _in ...

  3. vim diff 的使用

    vimdiff 是vim的比较工具可以对两个文件进行差异比较和快速合并 1. 使用vimdiff 比较两个文件 方式一 vimdiff  file_left  file_right   或者  vim ...

  4. Django之中间件-CSRF

    CSRF a.CSRF原理 post提交时需要提交csrf_token ,缺少则不通过 在form表单中加入: {% csrf_token %} b.无CSRF时存在隐患 防护其他人通过别的链接pos ...

  5. Oracle更新时间字段

    update field set BEGINDATE=to_date('2015-05-03 10:30:20','yyyy-mm-dd hh24:mi:ss') where NOO='01'  

  6. ASP.NET机制详细的管道事件流程(转)

    ASP.NET机制详细的管道事件流程 第一:浏览器向服务器发送请求. 1)浏览器向iis服务器发送请求网址的域名,根据http协议封装成请求报文,通过dns解析请求的ip地址,接着通过socket与i ...

  7. JQuery的一些思想,自己的一些见解!!!!

    自己总结了一下JQuery底层的一些思想,纯属于个人见解.. 为了方便描述,现在客户假如给了我们一个需求: 页面上有两个按钮,一张图片,当我点击hidden按钮时隐藏图片,当我点击show按钮时显示图 ...

  8. 如何编译文件(gcc + nasm)

    [0]README 编译文件(nasm + gcc),特别是编译目标文件的依赖文件:本文旨在回顾 gcc 和 nams 编译器的编译命令,以及如果有多个依赖文件该如何编译: [1]编译文件 1.1)编 ...

  9. Hive总结(四)hive安装记录

    本篇为安装篇较简单: 前提: 1: 安装了hadoop-1.0.4(1.0.3也能够)正常执行 2:安装了hbase-0.94.3, 正常执行 接下来,安装Hive,基于已经安装好的hadoop.过程 ...

  10. php eval()计算

    php中的eval()函数可以处理php代码,因此可以用此来解决:以字符串格式存储的计算公式 比如: $str='2*(3+12)'; $s=eval("return $str;" ...