TTTTTTTTTTTTTTTTTT POJ 1330
题意:给一个有根树,一个查询节点(u,v)的最近公共祖先;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=10000;
int dep[maxn+10],par[maxn+10];
int n,a,b,u,v;
vector<int> G[maxn+10]; void dfs(int cur,int d)
{
dep[cur]=d;
for(int i=0;i<G[cur].size();i++)
dfs(G[cur][i],d+1);
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) G[i].clear();
MM(par,0);
for(int i=1;i<=n-1;i++)
{
scanf("%d %d",&a,&b);
G[a].push_back(b);//降低复杂度,将n^2降至m
par[b]=a;
} int root;
for(int i=1;i<=n;i++)
if(!par[i]) {root=i;break;} dfs(root,1);//标记深度
scanf("%d %d",&u,&v); if(dep[u]<dep[v]) swap(u,v);
while(dep[u]>dep[v]) u=par[u]; while(u!=v)
{
u=par[u];
v=par[v];
}
printf("%d\n",u);
}
return 0;
}
分析:最基础的LCA
TTTTTTTTTTTTTTTTTT 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 ...
随机推荐
- 从零开始学习GDI+ (三) 画笔与画刷
- 100+ Python挑战性编程练习(1)
目前,这个项目已经获得了7.1k Stars,4.1k Forks. 初级水平是指刚刚通过Python入门课程的人.他可以用1或2个Python类或函数来解决一些问题.通常,答案可以直接在教科书中找 ...
- Packet flow in l2(receive and transmit)
Receive 1. napi && none napi 讲网络收报过程,必然要涉及到网卡收报模型发展历史.总体上看,网络收报过经历了如下发展过程: 轮询 ---à 中断 ---à ...
- linux源码下载
概要:本文主要介绍ubuntu环境下,内核源码和命令源码的获取方式. 内核源码: 1.最简洁的方式,使用命令:apt-get source linux-$(uname -r).但配置的源服务器中不一定 ...
- Python Set intersection() 方法
描述 intersection() 方法用于返回两个或更多集合中都包含的元素,即交集. 语法 intersection() 方法语法: set.intersection(set1, set2 ... ...
- 解决iframe缓存机制导致页面不清除缓存不刷新页面的bug
在使用iframe时,已有页面嵌套了一个iframe页面,当这个页面提交后再次跳转到本页面时,原本iframe内的页面应该刷新数据的,结果未刷新,需要清除缓存后才刷新. 解决方案: var fresh ...
- oracle常用函数(2)
1) trunc函数,用于截断数字, 截断数字,用法为:trunc(n1,n2),n1表示要被截断的数字,n2表示要截断到那位,但是不会进行四舍五入. n2还可以表示负数,表示截断到小数点前,意思就是 ...
- 定义一个接口CanFly,描述会飞的方法public void fly();
1.使用类与接口的知识完成如下要求:(1)定义一个接口CanFly,描述会飞的方法public void fly();(2)分别定义类飞机和鸟,实现CanFly接口.(3)定义一个测试类,测试飞机和鸟 ...
- 在easyui中解决使west和center为1:1,并且拖动窗口时能够自适应变化
<script type="text/javascript"> // 解决页面两个grid的布局问题 $(function(){// 在页面加载完毕后 //consol ...
- FileUtils.writeByteArrayToFile方法
FileUtil类是Apache Commons IO库里面的一个类,是与文件相关的一个辅助类,我写了一个可运行的java文件 import java.io.*; import org.apache. ...