题目大意:唔 就是给你一棵树 和两个点,问你这两个点的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】的更多相关文章

  1. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  2. POJ 1330 Nearest Common Ancestors(lca)

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

  3. POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20715   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(LCA 基于二分搜索+st&rmq的LCA)

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

  6. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  7. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

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

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

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

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

随机推荐

  1. 【数据分析 R语言实战】学习笔记 第十一章 对应分析

    11.2对应分析 在很多情况下,我们所关心的不仅仅是行或列变量本身,而是行变量和列变量的相互关系,这就是因子分析等方法无法解释的了.1970年法国统计学家J.P.Benzenci提出对应分析,也称关联 ...

  2. php类重载

    首先,因为PHP是弱类型语言,是不直接支持重载的,不过可以通过一些方法来实现重载. 先说一下重写 在子类继承父类方法后,自己重新定义父类的方法,包含函数名,形参(个数,顺序)完全相同,但权限修饰符可不 ...

  3. win7下如何解决协议适配器错误问题

    数据库为oracle 11g,在cmd中使用sqlplus命令出现了“协议适配器错误”. 原因分析:oracle相关服务没有启动. 解决办法如下: step1:进入服务页面. 方法一:cmd → se ...

  4. EJB2.0版本的HelloWorld

    EJB2.0版本的HelloWorld   虽然EJB3.1已经出来了,可是EJB2.0的项目还需要维护啊.下面写个简单EJB2.0的HelloWorld程序,练练手.   环境: JBoss 4.0 ...

  5. 成魔笔记1——先入IT,再成魔

    关于我为什么要写这个博客的原因,做一个简单的解释.因为报考的一时兴起,我选择了软件专业.可是三年下来,感觉自己没做多少事,也没收获到多少东西.很多时候都是老师讲什么,都是完全陌生的东西,跟不上教学的思 ...

  6. Kubernetes里的ConfigMap的用途

    顾名思义,ConfigMap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件. ConfigMap同Kubernetes的另一个概念secret类似,区别是ConfigMap主要 ...

  7. Vue相关问题

    1. 说一下Vue的双向绑定数据的原理 vue 实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫持各个属性的 setter, ...

  8. Html5怎么导出图片

    其实很简单, 首先需要两个js文件 jquery.min.js html2canvas.js 直接上代码,几行就解决了 <a id="example1" onclick=&q ...

  9. java B转换KB MB GB TB PB EB ZB

    public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...

  10. ubuntu 18.* 重启网卡

    systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting A ...