题目大意:唔 就是给你一棵树 和两个点,问你这两个点的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. sql中保留2位小数

    问题: 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round() 函数,如 Round(@num,2 ...

  2. sql server 2008怎么设置不允许windows身份验证

  3. Ambiguous mapping. Cannot map 'registerController' method

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappi ...

  4. WPF中窗体在同一个位置实现不同页面切换

    要想在WPF窗体中实现不同页面切换,我们就需要用到ContentControl这个控件,这个控件的位置和大小就是你要显示页面的位置和大小. 下面举例说明: Xaml: <Grid> < ...

  5. JS实现跑马灯效果(向左,向上)

    <html> <head> <title>JS实现跑马灯效果</title> <style> * { font-size:12px; fon ...

  6. [LUOGU] 1090 合并果子

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  7. python网络数据采集 Tesseract

    使用chrome代替PhantomJS,selennium3不支持PhantomJS,编码用"utf-8",不然会报错.tesseract要添加TESSDATA_PREFIX环境变 ...

  8. docker build no such file or directory

    在我构建新的镜像的时候, 发生 了  no such file or directory 的错误.  这个错误找了半天, 没头绪, 后来灵光一现, 原来是我的文件夹名字写错了 我的目录结构是这样的 [ ...

  9. 如何用纯 CSS 创作一个记事本翻页动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKOPGw 可交互视频教 ...

  10. (转)ios 代码规范

    转自http://blog.csdn.net/pjk1129/article/details/45146955 引子 在看下面之前,大家自我检测一下自己写的代码是否规范,代码风格是否过于迥异阅读困难? ...