题目:Nearest Common Ancestors

  根据输入建立树,然后求2个结点的最近共同祖先。

  注意几点:

  (1)记录每个结点的父亲,比较层级时要用;

  (2)记录层级;

  (3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时

代码:

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; const int M = ;
//vector<int> v[M];
int level[M];
int father[M]; void visitEveryone(vector<int> v[], int one, int _level)
{
level[one] = _level;
for(vector<int>::iterator it = v[one].begin(); it != v[one].end(); ++it)
{
visitEveryone(v, *it, _level+);
}
} int main()
{
int T,N,a,b,i,j;
scanf("%d", &T);
while(T--)
{
vector<int> v[M];
scanf("%d", &N);
memset(father, , (N+)*sizeof(int));
for(i = ; i < N; ++i)
{
scanf("%d %d", &a, &b);
father[b] = a;
v[a].push_back(b);
}
scanf("%d %d", &a, &b);
for(j=; father[j] > && j <= N; ++j); visitEveryone(v, j, ); while(a != b)
{ if(level[a] < level[b])
b = father[b];
else
a = father[a];
} printf("%d\n",a);
}
return ;
}

POJ 1330 Nearest Common Ancestors(Tree)的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  2. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  3. poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30147   Accept ...

  4. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  5. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  6. POJ 1330 Nearest Common Ancestors(裸LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39596   Accept ...

  7. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  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 Nearest Common Ancestors (dfs+ST在线算法)

    详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuang ...

随机推荐

  1. Ubuntu使用apt-get安装本地deb包

    我们都喜欢使用apt-get,因为它实在是让我们大大的省心.但是,有时候我们会为网速慢,安装源不好而烦恼,所以我们可能会将一些常用软件包的deb文件保存在本地以备不时之需.当然了使用dpkg也可以直接 ...

  2. linux 和 ecos 内核线程创建/信号量/event等对比

    ecos: int gx_thread_create (const char *thread_name, gx_thread_id *thread_id, void(*entry_func)(void ...

  3. 轻松使用Nginx搭建web服务器

    如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,“一哥”( ...

  4. VJP1193 扫雷(状压)

    链接 保存当前行和前一行两行的状态 #include <iostream> #include<cstdio> #include<cstring> #include& ...

  5. BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...

  6. wildfly9 配置SSL单向认证/https

    D:\>keytool -genkey -keystore cdi-init.keystore -alias cdi-init -keyalg RSA -keysize 2048 -validi ...

  7. 【转】下载太慢?简单设置让iTunes提速十几倍

    原文网址:http://www.startos.com/mac/ipad/tips/2010120713291.html 今年可以说是苹果欢笑的一年,ipad的发布,iphone4的成功,让用苹果设备 ...

  8. Android 应用启动渐变效果

    /** * 应用程序启动类:显示欢迎界面并跳转到主界面 * @author liux (http://my.oschina.net/liux) * @version 1.0 * @created 20 ...

  9. Hierarchy--分层。单词意思即为分层视图。

    英文释义:Hierarchy--分层.单词意思即为分层视图. 功能:层次Viewer允许你调试和优化您的用户界面.还可以学习别人做好的UI界面,它提供了一个布局的视图层次结构(布局视图)的视觉表现和放 ...

  10. 关于SQL\SQL Server的三值逻辑简析

    在SQL刚入门的时候,我们筛选为某列值为NULL的行,一般会采用如下的方式: SELECT * FROM Table AS T WHERE T.Col=NULL  www.2cto.com   而实际 ...