POJ 1330
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的更多相关文章
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
随机推荐
- OC-SEL
SEL SEL对应方法的地址 _cmd代表当前方法 1. 方法的存储位置 每个类的方法列表都存储在类对象中 每个方法都有一个与之对应的SEL类型的对象 根据一个SEL对象就可以找到方法的地址,进而调 ...
- MVC下的客户端模板技术
1.引言 在Web编程中,我们有时经常需要使用Ajax来访问服务端的接口,然后使用这些返回的数据(一般格式都是JSON)来展示客户端的相关信息.例如:在一个商品列表,我们点击某一样的商品,查看该商品的 ...
- github常见操作和常见错误!
本人总结: 1.问题: fatal: Not a git repository (or any of the parent directories) 解决: 本地库还没有创建,请先用git init ...
- Java 8 Optional类深度解析
身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只有在非空的前提下才能将其作为其他方法的参数. ...
- sql server 得到数据库字典
SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a. ...
- winScp如何通过隧道代理进行远程连接
目标机器:a ip为192.168.2.150(内网ip) a.xxx.x.xx(外网ip) 跳板机器:b ip为192.168.2.151(内网ip) b.xxx.xx.xx(外网ip) 目标机 ...
- codeforces #270 ABCD
Codeforces Round #270 A - Design Tutorial: Learn from Math 题意:给出n,求出两个合数x和y使x+y=n. 题解:暴力筛合数,然后暴力找 // ...
- CRect类
类CRect是对Windows结构RECT的封装,凡是能用RECT结构的地方都可以用CRect代替. 结构RECT表示一个矩形的位置和尺寸,其定义为: typedef struct tagRECT{ ...
- [译]JavaScript:将字符串两边的双引号转换成单引号
原文:http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html 代码的不一致性总是让人发狂,如果每位开发者都能 ...
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...