POJ 1330:Nearest Common Ancestors【lca】
题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么
思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上
#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#define maxn 10002
#define MOD 1000000007
using namespace std;
int head[maxn],point[maxn],next[maxn],father[maxn];
int now=0,in[maxn],finish=0;
bool visit[maxn];
inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
void add(int x,int y)
{
next[++now]=head[x];
head[x]=now;
point[now]=y;
}
int find(int x)
{
if(x==father[x])return x;
return father[x]=find(father[x]);
}
void dfs(int k,int s,int t)
{
for(int i=head[k];i;i=next[i])
{
if(finish==1)return;
int u=point[i];
dfs(u,s,t);
int x=find(k),y=find(u);
if(x!=y)father[y]=x;
}
visit[k]=1;
if(k==s && visit[t]){printf("%d\n",find(t));finish=1;}
else if(k==t && visit[s]){printf("%d\n",find(s));finish=1;}
return ;
}
int main()
{
int tt,n,x,y,root,s,t;
scanf("%d",&tt);
while(tt--)
{
now=finish=0;
n=read();
for(int i=1;i<=n;i++)father[i]=i;
for(int i=1;i<n;i++)
{
x=read(),y=read();
add(x,y);
in[y]++;
}
for(int i=1;i<=n;i++)if(in[i]==0)root=i;else in[i]=0;
s=read(),t=read();
dfs(root,s,t);
int u=sizeof(int)*(n+3);
int v=sizeof(bool)*(n+3);
memset(visit,0,v);
memset(head,0,u);
}
return 0;
}
POJ 1330:Nearest Common Ancestors【lca】的更多相关文章
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- 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 【最近公共祖先LCA算法+Tarjan离线算法】
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20715 Accept ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)
多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
随机推荐
- SVN中trunk,branches,tags用法详解【转】
Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-ta ...
- 分布式定时任务的redis锁实现
一个web项目如果部署为分布式时,平时常见的定时服务在一定的间隔时间内,可能出现多次重复调用的问题.而此时由于是不同容器之间的竞争,因此需要容器级别的锁 Redis为单进程单线程模式,采用队列模式将并 ...
- Ubuntu 14.04 配置confluence破解
1. 配置java环境,请参展我的另一篇博客 http://www.cnblogs.com/youran-he/p/8607155.html 2. 下载文件 https://pan.baidu.com ...
- 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- (五)VMware Harbor 部署之SSL
转自:https://www.cnblogs.com/Rcsec/p/8479728.html 1 .签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名 ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- 用例重试机制rerunfailures
安装 rerunfailures插件 pip install pytest-rerunfailures 使用: pytest --reruns 重试次数 如:pytest --reruns 2 重 ...
- 实现类似add(1)(2)(3)的函数
要求实现类似add(1)(2)(3)调用方式的方法,例如add为加法函数,则调用add(1)(2)输出3,调用add(1)(5)(3)输出9. 函数的调用方式是多次调用同一个函数,将每次传入的参数 ...
- 项目中多条数据保存的json实例
//js代码function checkCode(num){ var typeid = $("#typeid").val(); if(typeid == "") ...
- PAT 乙级 1005
题目 题目地址:PAT 乙级 1005 题解 本题主要就在于将一个数的一系列计算结果不重复地存储起来并便于检索,考虑到STL中的集合有相似的特性,使用set可以有效地简化代码和运算. 过程如下: (初 ...