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 ...
随机推荐
- asp.net读取excel文件多种方法
asp.net读取excel文件的三种方法示例,包括采用OleDB读取Excel文件.引用的com组件读取Excel文件.用文件流读取. 方法一:采用OleDB读取Excel文件 把Excel文件 ...
- js实现touch移动触屏滑动事件
在开始描述touch事件之前,需要先描述一下多触式系统中特有的touch对象(android和iOS乃至nokia最新的meego系统都模拟了类 似的对象).这个对象封装一次屏幕触摸,一般来自于手指. ...
- spring mvc中的文件上传
使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...
- JDBC基础一
JDBC:java database connectivity SUN公司提供的一套操作数据库的标准规范. JDBC与数据库驱动的关系:接口与实现的关系. JDBC规范(掌握四个核心对象): Driv ...
- C# 标准查询表达式
一.标准查询运算符 1.C#提供了标准查询运算符,例如我想选择专利一系列(pantents)中以年份19开头的专利,可以用如下语句: IEnumerable<Patent> pantent ...
- Learning Scrapy笔记(六)- Scrapy处理JSON API和AJAX页面
摘要:介绍了使用Scrapy处理JSON API和AJAX页面的方法 有时候,你会发现你要爬取的页面并不存在HTML源码,譬如,在浏览器打开http://localhost:9312/static/, ...
- WPF之旅(二)- XAML
什么是XAML XAML(Extensible Application Markup Lanaguage的简写,发音“zammel”)是用于实例化.NET对象的标记语言.尽管XAML是一种可以用于诸多 ...
- C# 将\u1234类型的字符转化成汉字
用代码获取网页的json数据时,经常会出现\u1234等字符,其实我们是知道他是汉字的 可以用下面的方法将\u1234翻译成汉字 /// <summary> /// /// </su ...
- 微信分享朋友链接显示js代码
通常自己做的一个页面想通过微信像朋友分享时,展示的标题和描述都是不是自己想要的,自己查了一些资料,原来是通过js来进行控制 展示效果如下: 标题.描述.还有分享的图片都是有js来控制的. js代码如下 ...
- ThinkPHP 3.2.2跨控制器调用方法
所谓跨控制器调用,指的是在一个控制器中调用另一个控制器的某个方法.在ThinkPHP中有三种方式实现跨控制器调用: 直接实例化: A()函数实例化; R()函数实例化. (1)直接实例化 直接实例 ...