版权声明:本文为博主原创文章,未经博主同意不得转载。

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】的更多相关文章

  1. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

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

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

  3. POJ 1470 Closest Common Ancestors 【LCA】

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

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

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

  5. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  6. POJ 1470 Closest Common Ancestors

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

  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: 20804   Accept ...

  9. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

随机推荐

  1. 安装SDK后打开安卓project后有例如以下错误:发现了以元素 &#39;d:skin&#39; 开头的无效内容。此处不应含有子元素。

    Error: Error parsing D:\Program Files\SDK\android-sdk-windows\system-images\android-22\android-wear\ ...

  2. Openlayer4 - 最好最强大的开源地图引擎

    Openlayer4 - 最好最强大的开源地图引擎 # githubhttps://github.com/openlayers/openlayers # 官网http://openlayers.org ...

  3. Win 7+Anaconda+tensorflow

    Anaconda是一种拥有各种Python库的集成环境,也支持Windows.Mac和Linux系统. 1.安装 Anaconda 安装包:https://www.continuum.io/downl ...

  4. SPOJ QTREE3 lct

    题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...

  5. protobuf 在win10系统如何编译jar包

    最近在搞java服务器项目,前段要求用protobuf进行数据传输,以前没搞过,查了很多资料,走了一些弯路! 先把一些需要下载的链接放上来: protobuf下载地址:https://github.c ...

  6. HDU - 5301 Buildings

    Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...

  7. innodb_flush_log_at_trx_commit和sync_binlog参数详解

    innodb_flush_log_at_trx_commit和sync_binlog参数详解         标签:                             innodb_flush_ ...

  8. 【Python + ATX】之uiautomator2 PageObject模式自动化框架学习

    参考文章: 感谢:cynic (linpengcheng) <ATX 基于 ATX-Server 的 UI 自动化测试框架> <ATX-uiautomator2 实现 webview ...

  9. 字符串截取mb_substr

    mb_substr("字符串","截取开始位置","截取个数","编码格式如UTF-8")

  10. js 把字符串当做方法执行

    <SCRIPT LANGUAGE="JavaScript"> function test(str){ alert(str); } eval('test("aa ...