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 ...
随机推荐
- 基于vant实现一个问卷调查
实现背景 最近学习<vue实战>,第二篇进阶篇有一个练习 刚好最近在研究vue移动端框架vant 两者结合,实现这么个小项目 实现代码 新建 vue单文件 L0529L.vue <t ...
- 2019ICPC徐州游记
裂开QAQ 热身赛听隔壁电科的猛男们说赛前别做题,结果我们3个憨憨还是跑到网吧打哈尔滨的重现赛.结果真的炸裂了,队友D被E题卡哭了,我和队友Z被I题搞炸. 回宾馆的路上都害怕明天裂开. 果然想什么坏事 ...
- Luogu P2612 [ZJOI2012]波浪
题目 我们考虑从\(1\)到\(n\)把每个数放到序列里面去,以消掉绝对值. 在最后的序列中,如果\(i\)的某一边是序列的边界,那么\(i\)会产生\(0\)的贡献.如果\(i\)的某一边是一个比\ ...
- linux源码下载
概要:本文主要介绍ubuntu环境下,内核源码和命令源码的获取方式. 内核源码: 1.最简洁的方式,使用命令:apt-get source linux-$(uname -r).但配置的源服务器中不一定 ...
- Python和mysql的连接
python与mysql的连接: 说明:前提是已近安装了mysql以及可视化工具(本人装的是Navicat) 1.在cmd下下载Python的第三方数据库包:pip install pymysql: ...
- 在 Chrome DevTools 中调试 JavaScript 入门
第 1 步:重现错误 找到一系列可一致重现错误的操作始终是调试的第一步. 点击 Open Demo. 演示页面随即在新标签中打开. OPEN DEMO 在 Number 1 文本框中输入 5. 在 N ...
- 32、出任爬虫公司CEO(爬取职友网招聘信息)
职友集,搜索到全国上百家招聘网站的最新职位. https://www.jobui.com/rank/company/ 打开网址后,你会发现:这是职友集网站的地区企业排行榜,里面含有 本 ...
- 修改文件夹的所有者为www
切换到root用户:su - root 修改文件所属用户和用户组:chown 用户:用户组 文件名如果用户和用户组是www,那么需要执行命令如下:chown www:www filename -R ( ...
- 32-第3章 数据链路层--抓包分析数据帧格式-ISO一图了然-小结
OSI理论模型 层级 名称 事物举例 功能 数据单位 别名 数据组成 协议举例 7 应用层 QQ.OA 网络通信 上层数据 上层数据 HTTP/FTP/DNS 6 表示层 web数据压缩.https加 ...
- zabbix布置mysql主从监控
一.下载安装zabbix服务器端 网址:https://www.cnblogs.com/carriezhangyan/p/10818513.html 二.下载安装zabbix客户端,客户端上要有mys ...