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. [Unity] UGUI研究院之游戏摇杆

    最近在做Unity基于UGUI的摇杆,发现了一种非常简单并且巧妙的方法,原文在这里, 不过要FQ!!http://godstamps.blogspot.tw/2015/07/unity-ugui-sc ...

  2. span设为inline-block之后,未包含文字时下面会多出一条空白问题

    1.问题的引出: 产品列表页面场景: 上面是产品图片[img], 中间是提示库存信息[span](始终存在,有库存则不显示文字,但元素占位.所以设置display:inline-block), 下面是 ...

  3. jquery access方法 有什么用

    Jquery设置对象属性的有几种方法1.获取属性attr(name) 2.设置属性attr(name,value)3.批量设置属性attr(properties)4.为所有匹配的元素设置一个计算的属性 ...

  4. Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法

    Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法 2014-03-27 11:44:46|  分类: Easy UI|举报|字号 订阅     可以使用$.parser.pa ...

  5. SAMBA 共享服务器搭建

    yum install samba service smb start chkconfig smb on 1.给要共享的文件夹赋权限 777 2.修改 smb 的配置文件:/etc/samba/smb ...

  6. [译]Mongoose指南 - Model

    编译你的第一个model var xxSchema = new Schema({name: 'string', size: 'string'}); var Tank = mongoose.model( ...

  7. SQL Server 服务器器信息备份(一)--login新建脚本备份

    前言 若你的企业使用SQL Server数据库镜像为容灾技术. 那你一定做过在镜像切换之前要新建Login,而且若Login密码不同,要修改链接数据库的字符串,在切换完之后则仍需要给数据库重新赋予权限 ...

  8. nyoj 10 skiing 搜索+动归

    整整两天了,都打不开网页,是不是我提交的次数太多了? nyoj 10: #include<stdio.h> #include<string.h> ][],b[][]; int ...

  9. XML理解

    XML:页面之间传递数据,跨平台传递,核心标签 HTML:超文本标记语言,核心标签 <xml version='1.0'>版本1.0<Nation> <one> & ...

  10. svn还原到指定版本

    svn还原到指定版本 1,选中文件夹,右健,show log 2,选中指定版本,右健,Revert to this revision 3,svn commit 4,ok