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 ...
随机推荐
- VS代码注释插件GhostDoc
http://community.submain.com/blogs/tutorials/archive/2013/03/28/how-to-access-ghostdoc-pro-features. ...
- centos docker
1.安装docker yum install docker --安装docker service docker start --启动docker服务 docker --version --查看版本 ...
- Linux Tools
WinSCP http://winscp.net/eng/download.php Xshell 5
- springboot+thymeleaf打war包在外部tomcat运行
工程目录 pom文件注意点 <packaging>war</packaging> <dependency> <groupId>org.springfra ...
- 内核调试神器SystemTap — 探測点与语法(二)
a linux trace/probe tool. 官网:https://sourceware.org/systemtap/ 探測点 SystemTap脚本主要是由探測点和探測点处理函数组成的,来看下 ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- nginx源码学习_数据结构(ngx_pool_t)
nginx中关于ngx_pool_t的数据结构位于src/core/ngx_palloc.c和src/core/ngx_palloc.h中,该数据结构主要是和内存池相关的,写下这篇博客前参考了网上很多 ...
- MAJOR-MINOR-MKDEV
标记一下,容易忘,以免每次查代码. ./include/linux/types.h:21:typedef __u32 __kernel_dev_t; ./include/linux/types.h:2 ...
- c++ [wrong]simple "Garbage Collector"
In fact, Ptr alone can accomplish the task mentioned below. Implementation see Ptr.h, main2.cpp. In ...
- 我的 Android 开发实战经验总结
我的 Android 开发实战经验总结 字数4440 阅读5137 评论43 喜欢250 曾经一直想写一篇总结 Android 开发经验的文章,预计当时的我还达不到某种水平,所以思路跟不上,下笔又捉襟 ...