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 ...
随机推荐
- Linux c实现一个tcp文件服务器和客户端
总体需求:编写tcp文件服务器和客户端.客户端可以上传和下载文件. ================================================ 分解需求 客户端功能描述: 1)要 ...
- tez
参考: 原理: https://www.cnblogs.com/rongfengliang/p/6991020.html https://www.cnblogs.com/hankedang/p/421 ...
- Oozie 3.3.1安装
软件安装路径 软件名称 版本 安装路径 jdk 1.6.0_12 /usr/java/jdk1.6.0_12 maven 3.1.0 /usr/local//apache-maven-3.1.0 Oo ...
- Linux中/etc下面passwd和shadow文件介绍
1./etc/passwd root@root:~# cat /etc/passwd root:x:::root:/root:/bin/bash daemon:x:::daemon:/usr/sbin ...
- 初识JavaScript(二)
初识JavaScript(二) 我从上一篇<初识JavaScript(一)>知道和认识JavaScript的词法结构,也开始慢慢接触到了JavaScript的使用方法,是必须按照JavaS ...
- python-2:爬取某个网页(虎扑)帖子的标题做词云图
关键词:requests,BeautifulSoup,jieba,wordcloud 整体思路:通过requests请求获得html,然后BeautifulSoup解析html获得一些关键数据,之后通 ...
- 下载MySQL的rpm包安装MySQL
cd /usr/local/src wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.27-1.el ...
- [转载]神经网络偏置项(bias)的设置及作用
[转载]神经网络偏置项(bias)的设置及作用 原文来自:https://www.cnblogs.com/shuaishuaidefeizhu/p/6832541.html 1.什么是bias? 偏置 ...
- java实现spark常用算子之cartesian
import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...
- 在springmvc中使用requestContextListener获取全部的request对象
RequestContextListener实现了 ServletRequestListener ,在其覆盖的requestInitialized(ServletRequestEvent reques ...