注意: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. SQLite可视化管理工具汇总

    版权声明:本文为博主原创文章,未经博主允许不得转载. 搜集了一些SQLite工具,在这里做个总结,有的工具用的多一些,有的只是简单试用,甚至未试用,所以有描述不当的还请回复指正,也欢迎补充完善! 20 ...

  2. webapi设置一个Action同时支持get和post请求

    代码如下: [AcceptVerbs("GET", "POST")] public HttpResponseMessage Http([FromUri]Prox ...

  3. Laravel 5.4---后端数据分页和前端显示分页结果

    后端数据(Eloquent 模型)分页 事先建立好Eloquent 模型和Controller 还有 前台的View.可以参考我之前的文章:Laravel建站03--建立前台文章列表和文章详情 在co ...

  4. S:33184777

    https://github.com/wjlWork/Crawl/tree/da0b63bc43bd7c238963f834a11df2581cecfb9c https://github.com/wh ...

  5. Spring Data JPA 事务锁

    1.概述 在本快速教程中,我们将讨论在Spring Data JPA中为自定义查询方法和预定义存储库的CRUD方法启用事务锁, 我们还将查看不同的锁类型并设置事务锁超时. 2.锁类型 JPA定义了两种 ...

  6. SpringMvc自动代理

    自动配置的好处是不需要挨个 实现[org.springframework.aop.framework.ProxyFactoryBean] ,只需要 advisor 配置和 <bean id=&q ...

  7. snmp默认团体名/弱口令漏洞及安全加固

    0x00基础知识 简单网络管理协议(SNMP)被广泛用于计算机操作系统设备.网络设备等领域监测连接到网络上的设备是否有任何引起管理上关注的情况.在运行SNMP服务的设备上,若管理员配置不当运行默认团体 ...

  8. Window下安装Gradle并在IDEA中配置

    真是作死啊,Maven都只是用得半瓢水,还来搞Gradle,小心撩得一身骚啊. 下载Gradle 下载页面为:https://gradle.org/releases/ Gradle 4.1的下载地址: ...

  9. go的timer定时器实现

    示例如下: package main import ( "fmt" "time" ) func testTimer1() { go func() { fmt.P ...

  10. mongodb的mongod.lock文件及oplog文件

    在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件.如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一 ...