ZOJ 1141 Closest Common Ancestors(LCA)
注意: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)的更多相关文章
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换
[简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...
- poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
随机推荐
- 在Linux下安装R语言软件
安装环境: centos.R3.0.1 1.在终端下下载: #cd /usr/local/ #wget http://mirror.bjtu.edu.cn/cran/src/base/R-3/R-3. ...
- mha安装报错 [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln361] None of slaves can be master. Check failover configuration file or log-bin settings in my.cnf
查找资料 参考 http://blog.51cto.com/16769017/1878451 解决方法: 在两个从库上开启二进制日志即可(花了 一天时间,找不到解决方法,最后还是靠自己的理解及测试解决 ...
- H5和CSS3新增内容总结
CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴影(text-shadow.) 边框: 圆角(border-rad ...
- Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...
- 基于JQuery实现表单元素值的回写
form.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GB ...
- c# emit 实现类的代理
using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; namespace Em ...
- 让uboot的tftp支持上传功能
转载:http://blog.chinaunix.net/uid-20737871-id-2124122.html uboot下的tftp下载功能是非常重要和常见的功能.但是偶尔有些特殊需求的人需要使 ...
- cacti 主机/网络设备流量监控 图解
1.在配置中找到设备 console —> Device 2.初次添加 cacti 监控主机的时候是没有任何设备的,所以要选择add 添加你要监控的主机 \
- load-on-startup 解释
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" &qu ...
- iOS --生产JSON格式,创建JSON文件,创建文件夹,指定储存
//生成json文件 - (void)onjson { // 如果数组或者字典中存储了 NSString, NSNumber, NSArray, NSDictionary, or NSNull ...