POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
https://blog.csdn.net/u013912596/article/details/35311489
题目链接:http://poj.org/problem?id=1470
题目大意:给出一棵树。再给出若干组数(a,b),输出节点a和节点b的近期公共祖先(LCA)
就是非常裸的LCA。可是我用的是《挑战程序设计竞赛》上的“基于二分搜索的算法求LCA”,我看网上用的都是tarjan算法。可是我的代码不知道为什么提交上去 wrong answer,自己想的非常多測试数据也都和题解结果一样,不知道错在哪里,所以把代码保存一下。留待以后解决。。。。
。。
假设读者有什么建议。希望提出来。感激不尽!
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 911
#define LOG_N 11
int times[N];
bool findroot[N];
#include <vector>
vector<int> g[N];//
int root;
int parent[LOG_N][N];
int depth[N];
void dfs(int v,int p,int d)
{
parent[0][v]=p;
depth[v]=d;
for(int i=0;i<g[v].size();i++)
{
if(g[v][i]!=p) dfs(g[v][i],v,d+1);
}
}
//预处理
void init(int V)//预处理出parent
{
dfs(root,-1,0);
for(int k=0;k+1<LOG_N;k++)
{
for(int v=1;v<=V;v++)
{
if(parent[k][v]<0) parent[k+1][v]=-1;
else parent[k+1][v]=parent[k][parent[k][v]];
}
}
}
//计算u和v的LCA
int lca(int u,int v)
{
//让u和v向上走到同一深度
if(depth[u]>depth[v]) swap(u,v);
for(int k=0;k<LOG_N;k++)
{
if((depth[v]-depth[u])>>k&1)
{
v=parent[k][v];
}
}
if(u==v) return u;
//利用二分搜索计算LCA
for(int k=LOG_N-1;k>=0;k--)
{
if(parent[k][u]!=parent[k][v])
{
u=parent[k][u];
v=parent[k][v];
}
}
return parent[0][u];
}
int main()
{
freopen("D:/in.txt","r",stdin);
int n,t,a;
while(~scanf("%d",&n))
{
char ch1[2],ch2[2];//吸收掉那个可恶的括号什么的东西
memset(times,0,sizeof(times));
memset(parent,0,sizeof(parent));
memset(depth,0,sizeof(depth));
memset(findroot,0,sizeof(findroot));
for(int i=1;i<=N;i++)
g[i].clear();
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&a,&t);
for(int j=0;j<t;j++)
{
int temp;
scanf("%d",&temp);
g[a].push_back(temp);
findroot[temp]=true;
}
}
for(int i=1;i<=n;i++)
if(!findroot[i])
{
root=i;
break;
}
init(n);
int qn,fir,sec;
scanf("%d",&qn);
for(int i=0;i<qn;i++)
{
scanf("%1s%d%d%1s)",ch1,&fir,&sec,ch2);
times[lca(fir,sec)]++;
}
for(int i=1;i<=n;i++)
{
if(times[i])
printf("%d:%d\n",i,times[i]);
}
printf("\n");
}
return 0;
}
POJ 1470 Closest Common Ancestors【近期公共祖先LCA】的更多相关文章
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- 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 ...
- POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- 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: 20804 Accept ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
随机推荐
- 移动端H5实现图片上传
移动端H5实现图片上传 https://segmentfault.com/a/1190000010034177
- iOS_绘制带删除线的Label
效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...
- placeholder 不支持进行兼容处理
;(function () { //全局ajax处理 $.ajaxSetup({ complete: function (jqXHR) {}, data: { }, error: function ( ...
- java 无状态和有状态区别
诸位Java程序员,想必大家对SimpleDateFormat并不陌生.不过,你是否知道,SimpleDateFormat不是线程安全的(thread safe).这意味着,下面的代码是错误的: ...
- 一起talk C栗子吧(第八十七回:C语言实例--使用管道进行进程间通信概述)
各位看官们,大家好.上一回中咱们说的是进程间通信的样例.这一回咱们说的样例是:使用管道进行进程间通信. 闲话休提,言归正转. 让我们一起talk C栗子吧! 我们在前面的的章回中介绍了使用管道进行进程 ...
- innodb_flush_log_at_trx_commit和sync_binlog参数详解
innodb_flush_log_at_trx_commit和sync_binlog参数详解 标签: innodb_flush_ ...
- mySQL 开启事件存储过程
怎样在Navicat中设置,是数据库按照记录中的日期更新状态字段 其实这个很常用,比如你网站里的某条记录的日期——比如说数据库中某条活动记录的审核日期字段已经过期,亦即当前时间已经超过审核日期,那么定 ...
- 我的第二个java程序 循环
public class Test {//类 public Test (int num){//构造方法,和类同名,无返回值,接收传参并定义传参的类型,大小写敏感 int x = 10;//局部变量,定 ...
- jQuery一步一步实现跨浏览器的可编辑表格,支持IE、Firefox、Safari、
脚 本 之 家 www.jb51.net 脚本云 专题 素材下载 电子书 软件下载 源码下载 服务器常用软件 a5交易 首页 网页制作 脚本专栏 脚本下载 网络编程 数据库 CMS教程 电子书籍 平面 ...
- Microsoft SQL Server JDBC 驱动程序支持矩阵
本页包含 Microsoft SQL Server JDBC 驱动程序的支持矩阵和支持生命周期策略. Microsoft JDBC 驱动程序支持生命周期矩阵和策略 Microsoft 支持生命周期 ( ...