题意:给一个有根树,一个查询节点(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的更多相关文章

  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 ...

随机推荐

  1. private/默认/protected/public权限修饰符的区别

    private/默认/protected/public权限修饰符和面向对象的三大特性的封装性有着密切关系.它们都可以修饰类的成员,其中的默认和public还可以修饰类. 类的成员包括:成员变量.成员方 ...

  2. cocos creator 3D | 拇指投篮 | 3D项目入门实战

    你的命中率是多少呢?文章底部试玩! 效果预览 配置环境: Cocos Creator 3D v1.0.1 玩法说明: 触摸屏幕,向上滑动投篮!注意篮板是会移动的哦!看看你的命中率是多少! 实现原理 为 ...

  3. Spring是如何使用责任链模式的?

    关于责任链模式,其有两种形式,一种是通过外部调用的方式对链的各个节点调用进行控制,从而进行链的各个节点之间的切换. 另一种是链的每个节点自由控制是否继续往下传递链的进度,这种比较典型的使用方式就是Ne ...

  4. 洛谷 P3857 彩灯 题解

    题面 对于每一个开关,我们可以看成一个0/1串,初始是一个全部为0的串,要求经过这些开关的操作后,出现的不同的0/1串的个数 建模就是存在一些数,这些数异或起来是0(等价于没有操作).那么需要求一个集 ...

  5. Luogu P4147 玉蟾宫

    题目 就是全0子矩阵. 先预处理每个点上面有多少个连续的0(包括自己). 然后我们枚举下边界(1-n). 我们开一个单调栈,记录一个上界递增的矩形集合. 如果我们扫到了一个比当前栈顶要矮的矩形,那么我 ...

  6. 【golang】浅析rune数据类型

    golang中string底层是通过byte数组实现的.中文字符在unicode下占2个字节,在utf-8编码下占3个字节,而golang默认编码正好是utf-8. golang中还有一个byte数据 ...

  7. php程序Apache,IIS 7,nginx 伪静态配置方法总汇

    一,Apache 环境伪静态配置方法: 在根目录下放置一个.htaccess 文件,内容如下: <IfModule mod_rewrite.c> Options +FollowSymlin ...

  8. ubuntu部署Java、Python开发环境

    要部署Java开发环境首先就要安装JDK. 一.安装JDK8 1. 下载 jdk-8u172-linux-x64.tar.gz 到 /usr/java8/ 目录下: 2. tar  -zxvf  jd ...

  9. 2019-11-29-git-需要知道的1000个问题

    title author date CreateTime categories git 需要知道的1000个问题 lindexi 2019-11-29 8:36:7 +0800 2018-2-13 1 ...

  10. java面试题全集(上)

    1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...