题目链接: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. (转)PHP的语言结构和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...

  2. 使用mitmf 来绕过HSTS站点抓取登陆明文

    使用mitmf 来绕过HSTS站点抓取登陆明文 HSTS简介 HSTS是HTTP Strict Transport Security的缩写,即:"HTTP严格安全传输".当浏览器第 ...

  3. 通过Eclipse创建SQLite数据库

    import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database ...

  4. delphi 资源文件详解

    delphi资源文件详解 一.引子: 现在的Windows应用程序几乎都使用图标.图片.光标.声音等,我们称它们为资源(Resource).最简单的使用资源的办法是把这些资源的源文件打入软件包,以方便 ...

  5. MHA在线切换过程

    MHA 在线切换是MHA除了自动监控切换换提供的另外一种方式,多用于诸如硬件升级,MySQL数据库迁移等等.该方式提供快速切换和优雅的阻塞写入,无关关闭原有服务器,整个切换过程在0.5-2s 的时间左 ...

  6. git传输协议原理

    git精神:distributed-is-the-new-centralized 转自:http://git-scm.com/book/zh/v1/Git-%E5%86%85%E9%83%A8%E5% ...

  7. [转载]--类unix系统如何初始化shell

    Shell的配置文件 当我们在linux中打开一个shell的时候,系统会读取相关的配置文件来初始化shell(其他unix-like OS也一样).配置文件包括以下这些: 1. 全局配置(对所有用户 ...

  8. 时隔3年半Spring.NET 2.0终于正式Release了

    一直很喜欢Spring.NET,不过2011年8月2日1.3.2正式release之后,再没有正式版本的release了. 直到4天前,Spring.NET 2.0 GA终于Release. http ...

  9. [转]IIS部署托管管道模式的集成和经典区别

    关于ESPS和SCSJ在Windows server 2008的问题总结 SCSJ出现的问题在于集成模式和经典模式的选择上,系统本身是没有问题的.我们在部署系统的时候,选择了集成模式,导致WebCon ...

  10. [转]Asp.net三种事务处理

    事务处理是在数据处理时经常遇到的问题,经常用到的方法有以下三种总结整理如下:方法1:直接写入到sql 中在存储过程中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRA ...