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. standford工具-parser

    stanford自然语言处理开源了很多工具,很实用也很方便,记录下来,以备后用. 第一篇就从句法分析开始吧(所用的平台都是java+eclipse). <一>操作 1.http://www ...

  2. Django 部署

    话说这个部署挺折腾人的,先开始使用 mod_python ,貌似版本一直有问题,没成功过,以后再试. 使用 mod_wsgi 成功,记录如下: 1.下载 mod_wsgi: http://code.g ...

  3. 理解CSS3中的background-size(对响应性图片等比例缩放)

    理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...

  4. 谈谈我眼中的CSDN吧

    昨天逛博客园看到了这篇曝光率很高的文章:博客搬家——从CSDN到博客园,一篇短短的文章竟然招致这么多人的讨论,可能程序员就喜欢“Java好还是PHP好”这类型的问题吧,好无聊.由于我一直在使用CSDN ...

  5. 如何判断retina,如何判断设备的比例

    http://www.189works.com/article-95647-1.html 说起iPhone 4带来的革新,retina display绝对是最吸引眼球的一项,以至于我现在看电脑的显示屏 ...

  6. jelq

    初级 The Newbie Routine 5 minutes hot wrap 5 minutes manual stretch (ten 30-second stretches) 10 minut ...

  7. Office 2010/2007 简繁体转换按钮不见了?

    注:此文章来自微软官方,原文链接:http://support.microsoft.com/kb/2459493/zh-tw 经测试可解决问题.——————————– 通常发生这样的问题,是由于繁简转 ...

  8. APPCAN IDE中安装emmet插件

    1.首先打开APPCAN IDE 2.帮助(help)-安装新软件(install New sofaWare) 3.打开Install窗口,点击 Add,在Add Repository窗口中,Name ...

  9. javascript高级程序设计---Event对象

    事件是一种异步编程的实现方式,本质上是程序各个组成部分之间传递的特定消息. DOM的事件操作(监听和触发),都定义在EventTarget接口 该接口就是三个方法,addEventListener和r ...

  10. SQL按指定文字顺序进行排序(中文或数字等)

    在有些情况下我们需要按指定顺序输出数据,比如选择了ID in(3,1,2,5,4)我们希望按这个3,1,2,5,4的顺序输出,这样只使用order by ID是无法实现的, 但是我们可以使用order ...