http://poj.org/problem?id=1330

题意:给你一棵树的上的两个点,要你求这两个点的最近的父亲节点。

第一行的是m案例数

第二行给你个N,代表有N-1种父子关系,其中a b,a是b的父亲。

第N行就是要你求这两个点的最近的父亲节点。

思路:很简单,不用discuss里面的那些,就直接一个递归寻找出第一个点的所以父亲节点,并做好标记,然后用递归寻找第二个点的父亲节点,如果碰到了标记的话,那么这个点就是他们的最近的那个父亲节点。

 #include <stdio.h>
#include <string.h>
#define l 100100 bool mark[l];
int belg[l],ans,flog; int bfs(int x)
{
if(mark[x]) {
mark[x]=false;
if(!belg[x]) return ;
bfs(belg[x]);
}
return ;
}
int bfs1(int x)
{
if(belg[x]&&mark[x]) bfs1(belg[x]);
if(!mark[x]&&!flog){
ans=x;
flog=;
return ;
}
return ;
} int main()
{
int m,n,a,b;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
memset(belg,,sizeof(belg));
memset(mark,true,sizeof(mark));
for(int i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
belg[b]=a;
}
scanf("%d%d",&a,&b);
flog=;
bfs(a);
bfs1(b);
printf("%d\n",ans);
}
return ;
}

POJ 1330的更多相关文章

  1. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  2. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  3. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  4. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  5. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  6. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  7. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  8. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  9. POJ 1330 LCA裸题~

    POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...

  10. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

随机推荐

  1. CentOS7 mono环境连接WCF

    总结下在mono环境中使用Wcf出现的问题以帮助你快速解决问题. 1.昨天在内网Centos7下部署由Windows上开发完成的ASP.NET Mvc4项目,部署到mono环境下遇到了无法找到endp ...

  2. [js/jquery]移动端手势拖动,放大,缩小预览图片

    摘要 有这样的需求需要在手机端预览图片的时候,实现图片的手势拖动,放大缩小功能.最终通过touch.js这个插件实现了效果. touch.js Touch.js是移动设备上的手势识别与事件库, 由百度 ...

  3. vim插件ctags的安装和使用

    vim插件ctags的安装和使用 2013-11-19 20:47 17064人阅读 评论(0) 收藏 举报  分类: 开发工具(3)  linux编程(9)  c/c++编程(11)  版权声明:本 ...

  4. Java Programming Test Question 4

    What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...

  5. 关于精简安装office2010的步骤

    首先我们安装系统都会安装一个办公套件:office当然你也可以选择使用wps但我个人比较讨厌wps的广告 然而使用office完整安装不仅消耗资源大启动速度慢,而且一些功能我都用不到,所以我这几来一个 ...

  6. lwfs指定特定目录输出

    在特定节点启lwfs服务,输出特定的目录 在[root@devcpucs ~]# 节点启lwfs服务,输出指定目录/home/export/online1/systest/swcpucs 1.将gio ...

  7. USB协议[转]_基本上涵盖了所有最基础的USB协议相关知识。

    背景: 需要使用到USB协议,我一直尝试着去强记这个流程,现在看来,其实不用.看多了,把这个过程具象出来,就牢牢记住了. 正文: 正文转自:http://fangjian0518.blog.163.c ...

  8. informatica中元数据管理

    摘自: http://blog.itpub.net/28690368/viewspace-766528/ informaica是一个很强大的ETL工具,WORKFLOW MANAGER负责对ETL调度 ...

  9. jersey

    http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html

  10. Javascript高级程序设计——面向对象小结

    ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...