注意: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. typedef 与 define 的区别

    1.区别 (1)定义.执行时间.作用域 定义.执行时间: #define pchar char * typedef char *pchar; 定义的格式差别,显而易见的,要注意,define 是不能存 ...

  2. iOS 一些struct类型的NSLog输出

    我们经常会输出一些坐标尺寸信息之类的,比如view的frame,是CGRect类型的,用frame.oringial.x 和frame.size.width来做NSLog参数好麻烦,还好苹果对这些常用 ...

  3. spring boot 发布成包所需插件

    在pom.xml里配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

  4. 基于友善之臂ARM-tiny4412--uboot源代码分析

    /* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...

  5. checkStyle使用具体解释

    简单介绍 checkStyle是一款代码格式检查工具.它依据设置好的编码规则来自己主动检查代码.比方命名规范,文件长度.代码行长度等等.代码检查工具是保证项目代码质量.统一编码风格的一种重要途径.本篇 ...

  6. Gitlab来做代码review

    Gitlab来做代码review 代码review是代码质量保障的手段之一,同时开发成员之间代码review也是一种技术交流的方式,虽然会占用一些时间,但对团队而言,总体是个利大于弊的事情.如何借助现 ...

  7. erlang的随机数 及 random:uniform()函数

    每次调用会更新进程字典里的random_seed变量,这样在同一个进程内每次调用random:uniform()时,随机数种子都不同,所以生成的随机数都不一样(调用完random:uniform()后 ...

  8. XP,32/64位Win7,32/64位Win10系统【电脑城版】

    本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...

  9. iOS 应用发布

    本文转载至  http://blog.csdn.net/ysy441088327/article/details/7833579 苹果为广大的开发者提供了一个很好的应用生态环境 参考资料: 1:如何向 ...

  10. share初始化

    要看懂share先看与map的交互以及跨地图的交互 share初始化 void Share::ShareInit(I_DataLayer* data_layer) { // 加载xls表 if(!Lo ...