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 ...
随机推荐
- 【Python】合并(拼接)字符串
在很多情况下,我们都需要合并字符串.例如:需要将姓氏与名字存储在不同的变量中,然后显示的时候再将他们合二为一 first_name = 'oliver' last_name = 'smith' ful ...
- Oracle在plsql中修改数据
Oracle在plsql中想要修改数据,有两种方式: a.使用rowid+点击锁图标,语句为: select t.*,rowid from T_BIC_PLY_MAIN t; b.使用for up ...
- Log4net日志记录、详细配置(自己使用>)
log4net库是Apache log4j框架在Microsoft.NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具 1.首先添加对log4net.dll的引 ...
- ASP.NET数据库连接字符串的加密与解密
ASP.NET web.config中,数据库连接字符串的加密与解密. 虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容 加密: ...
- 卡特兰数-Catalan数
卡特兰数的含义: 说到卡特兰数,就不得不提及卡特兰数序列.卡特兰数序列是一个整数序列.其通项公式是我们从中取出的就叫做第n个卡特兰数数,前几个卡特兰数数是:1, 1, 2, 5, 14, 42, 13 ...
- 一条长l的笔直的街道上有n个路灯,若这条街的起点为0,终点为l,第i个路灯坐标为ai,每盏灯可以覆盖到的最远距离为d,为了照明需求,所有灯的灯光必须覆盖整条街,但是为了省电,要是这个d最小,请找到这个最小的d。
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- Net中的代码规范工具及使用
Net中的代码规范工具及使用 https://www.cnblogs.com/selimsong/p/9209254.html 上一篇文章介绍了编码标准中一些常用的工具,本篇就具体来介绍如何使用它们来 ...
- 【demo练习四】:WPF用户控件案例
首先,新建vs中“用户控件(WPF)”,右键项目名 =>"添加"按钮 => 选择“新建项”. 然后选择“用户控件(WPF)” => 起名字 => 点击“添加 ...
- [原]js获取dom元素的实际位置及相对坐标
关键API: Element.getBoundingClientRect() mdn:https://developer.mozilla.org/en-US/docs/Web/API/Element/ ...
- 各种jar包下方法的使用
commons-codec-1.6.jar: DigestUtils.md5Hex(String str); httpclient-4.2.2.jar: HttpClient client=new D ...