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

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. 使用Google Https搜索

    一. 将浏览器Google的默认搜索使用https 1. IE浏览器 进入注册表搜索"google.com"将搜索替换成https://www.google.com.hk 2. F ...

  2. 开源管理系统OSSIM设置 语言为中文简体

    最近研究OSSIM系统,OSSIM的安装是做好的ISO,操作系统选择的是CentOS 64Bit系统.我使用的OSSIM 4.11 的ISO安装,虽然系统说明支持中文,实际上,只是台湾的繁体中文而以. ...

  3. Atitit。sql2016标准化的规划方案 v3 q2a

    Atitit.sql2016标准化的规划方案 v3 q2a 1. Sql标准化的历史3 1.1. Sql92标准4 1.2. Sql99标准4 1.3. SQL:2003为例,它包括以下9个部分 5 ...

  4. Python内置函数之classmethod()

    函数的参数是一个函数: classmethod(func) 作用是,在外部,类对象能够直接调用类方法. 常用来作为装饰器. >>> class C: ... def f(self): ...

  5. K均值算法-python实现

    测试数据展示: #coding:utf-8__author__ = 'similarface''''实现K均值算法 算法摘要:-----------------------------输入:所有数据点 ...

  6. ../lib//libscsdblog.so: undefined reference to `pthread_atfork'

    代码中遇到这个问题,但是在makefile中已经添加了-lpthread. 最后发现问题时,引入库的顺序,把-lpthread放在最后就可以了.

  7. python简单C/S模式示例

    服务器端代码: #!/usr/bin/python import time, socket, threading # thread handle function def tcplink(sock, ...

  8. Hbuilder MUI 注册短信验证60秒后重新发送

    <div class="mui-input-row"> <label class="iconfont_log_reg icon-youjian" ...

  9. Spectral Graph Theory的一些定理

    邻接矩阵的特征值和特征向量不会随着节点的排列不同而变化.两个图同构可以推出他们的邻接矩阵具有相同的特征值和特征向量,但是反过来不行.

  10. 【转】Web前端开发:为何选择MVVM而非MVC

    在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的技术原因,MVC在Web前端开发中根本无法使用(对的,是无法,而不是不该) 在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的 ...