题目链接: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的更多相关文章

  1. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

  2. POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)

    其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...

  3. POJ 1470 Closest Common Ancestors LCA题解

    本题也是找LCA的题目,只是要求多次查询.一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 由于查询的数据会 ...

  4. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  5. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  6. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  7. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  8. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  9. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

随机推荐

  1. K-Means(K均值)算法

    昨晚在脑内推导了一晚上的概率公式,没推导出来,今早师姐三言两语说用K-Means解决,太桑心了,昨晚一晚上没睡好. 小笨鸟要努力啊,K-Means,最简单的聚类算法,好好实现一下. 思路: 共有M个样 ...

  2. c# 如何获取项目的根目录

    c# 如何获取项目的根目录 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设 ...

  3. 二、MongoDB的基础知识简介

    1.文档.集合和数据库 a).文档:因为MongoDB是面向文档的数据库,那么可想而知文档是它的基本单元,相当于关系型数据库中的行! Ⅰ.它是由键值对组成的一个有序集:注:键不能为空且是字符串类型的. ...

  4. android任意view爆炸效果--第三方开源--ExplosionField

    犹如天女散花一样,爆炸散列,比较有趣.Android ExplosionField在github上的项目主页是:https://github.com/tyrantgit/ExplosionField ...

  5. 光迁PING值延迟计算!以及到中国最快的美国机房是哪个机房?

    美国圣安娜KT机房/美国KT机房/美国KT服务器 KT机房是美国直达大陆最快的机房,ping值一般为195MS,是做web服务器的首选机房,深受中小站长的欢迎! 我们平时测试美国服务器的速度,都是通过 ...

  6. IE PNG格式的图片不现实的的解决方法

    可能是安装某些软件导致注册表缺失png的一些设置 ,网上找了好些办法都是修改注册表的. 终于找到傻瓜式的解决方法:将下面的代码复制到txt中 另存为reg后缀格式,双击运行即可,然后重新打开IE 完事 ...

  7. java解析XML几种方式

    第一种:DOM. DOM的全称是Document Object Model,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序 ...

  8. 浅议iOS网络数据解析

    /*------------------------------------ 数据解析: 1.JSON数据 --------------------------------*/ 重点:1.什么是JSO ...

  9. c,c++函数返回多个值的方法

    最近遇到一个问题,需要通过一个函数返回多个值.无奈C,C++不能返回多个值.所以就想有什么方法可以解决. 网上方法比较杂乱,一般有两种替代做法: 1. 利用函数的副作用, 返回值在函数外定义, 在函数 ...

  10. 插值和空间分析(二)_变异函数分析(R语言)

    方法1.散点图 hscat(log(zinc)~, meuse, (:)*) 方法2.变异函数云图 library(gstat) cld <- variogram(log(zinc) ~ , m ...