poj 1470 Closest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1470
Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)
题目描述:给出一棵树的节点之间的信息,然后给出一些询问,每次询问是求出两个节点的LCA。统计每个节点被作为询问中LCA的次数并输出。
算法分析:LCA离线算法,从算法思想和思维上并不难,可以说是一道模板题,但这道题的输入有点麻烦,处理一下输入就可以了。
另外ZOJ1141也是这道题,我在ZOJ上面AC了
但是在POJ上WA了,最后处理输入之后AC了。
看看程序想了想,应该POJ上面形如5:(5)这样的输入中有可能不是冒号和括号,而是其他的符号吧。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int max_log_maxn=; int n,root;
vector<int> G[maxn];
int father[max_log_maxn][maxn],d[maxn];
int vis[maxn]; void dfs(int u,int p,int depth)
{
father[][u]=p;
d[u]=depth;
int num=G[u].size();
for (int i= ;i<num ;i++)
{
int v=G[u][i];
if (v != p) dfs(v,u,depth+);
}
} void init()
{
dfs(root,-,);
for (int k= ;k+<max_log_maxn ;k++)
{
for (int i= ;i<=n ;i++)
{
if (father[k][i]<) father[k+][i]=-;
else father[k+][i]=father[k][father[k][i] ];
}
father[k][root]=root;
}
} int LCA(int u,int v)
{
if (d[u]>d[v]) swap(u,v);
for (int k= ;k<max_log_maxn ;k++)
{
if ((d[v]-d[u])>>k & )
v=father[k][v];
}
if (u==v) return u;
for (int k=max_log_maxn- ;k>= ;k--)
{
if (father[k][u] != father[k][v])
{
u=father[k][u];
v=father[k][v];
}
}
return father[][u];
} int main()
{
while (scanf("%d",&n)!=EOF)
{
int u,num,v;
for (int i= ;i<=n ;i++) G[i].clear();
memset(vis,,sizeof(vis));
char str[];
memset(str,,sizeof(str));
char ch[],ch2[],ch3[];
for (int i= ;i<n ;i++)
{
int u=,num=;
scanf("%d%1s%1s%d%1s",&u,ch,ch2,&num,ch3);
// scanf("%s",str);
// int u=0,num=0;
// int len=strlen(str);
// int j=0;
// for (j=0 ;j<len && str[j]!=':';j++) u=u*10+str[j]-'0';
// for (j=j+2 ;j<len && str[j]!=')';j++) num=num*10+str[j]-'0';
while (num--)
{
scanf("%d",&v);
G[u].push_back(v);
vis[v]=;
}
}
for (int i= ;i<=n ;i++) if (!vis[i]) {root=i;break; }
init();
memset(vis,,sizeof(vis));
scanf("%d",&num);
memset(str,,sizeof(str));
//getchar();
for (int j= ;j<num ;j++)
{
scanf("%1s%d%d%1s",ch,&u,&v,ch2);
// gets(str);
// int u=0,v=0;
// int len=strlen(str);
// int i=0;
// for (i=1 ;i<len && str[i]>='0' && str[i]<='9' ;i++)
// u=u*10+str[i]-'0';
// while (i<len)
// {
// if (str[i]>='0' && str[i]<='9') break;
// i ++ ;
// }
// while (i<len)
// {
// if (str[i]==')') break;
// v=v*10+str[i]-'0';
// i ++ ;
// }
int ans=LCA(u,v);
vis[ans]++;
}
for (int i= ;i<=n ;i++)
if (vis[i]) printf("%d:%d\n",i,vis[i]);
}
return ;
}
poj 1470 Closest Common Ancestors LCA的更多相关文章
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...
- POJ 1470 Closest Common Ancestors LCA题解
本题也是找LCA的题目,只是要求多次查询.一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理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 ...
- 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 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
随机推荐
- kettle的windows安装
1.首先去官网下载安装包,这个安装包在所有平台上是通用的. 2.kettle是java语言开发的,所以需要配置JAVA_HOME 3.解压kettle的安装包 4.配置环境变量,KETTLE_HOME ...
- habse的CopyTable
需求:对hbase的一张表进行拷贝 一.table1的内容如下 hbase(main)::> scan 'table1' ROW COLUMN+CELL column=f1:age, times ...
- mysql查询语句(mysql学习笔记七)
Sql语句 一般顺序GHOL : group by,having ,order by,limit 如果是分组,应该使用对分组字段进行排序的group by语法 ...
- Yii-数据模型- rules类验证器方法详解
public function rules(){ return array( array('project_id, type_id, status_id, owner_id, requester_id ...
- C-链表的一些基本操作【创建-删除-打印-插入】
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define LEN sizeof(stru ...
- DIV JS CSS 轻量级弹出层 兼容各浏览器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在Linux 5/6上使用UDEV SCSI规则配置ASM DISK
格式化磁盘(略) 识别磁盘(/sbin/scsi_id) Oracle Linux 5用如下脚本: #!/bin/sh for i in b c d e f g do echo "KERN ...
- Hadoop1.x与2.x安装笔记
Hadoop1.x与2.x安装笔记 Email: chujiaqiang229@163.com 2015-05-09 Hadoop 1.x 安装 Hadoop1.x 集群规划 No 名称 内容 备注 ...
- 第六节:宿主如何使用AppDomain
前面已经讨论了宿主以及宿主加载CLR的方式.同时还讨论了宿主如何告诉CLR创建和卸载AppDomain.为了使这些讨论更加具体,下面将描述一些常见的宿主和AppDomain使用情形.特别地,我要解释不 ...
- Linux驱动开发之字符设备模板
/***************************** ** 驱动程序模板* 版本:V1* 使用方法(末行模式下):* :%s/xxx/"你的驱动名称"/g********* ...