题目大意:唔 就是给你一棵树 和两个点,问你这两个点的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;

}

转载于:https://www.cnblogs.com/philippica/p/4155005.html

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 / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)...

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  3. poj 1330 Nearest Common Ancestors LCA/DFS

    题目链接: http://poj.org/problem?id=1330 题意: 求出两点间的最近公共祖先. 题解: 第一种: 并查集维护:http://www.cnblogs.com/procedu ...

  4. POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)

    题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...

  5. [POJ 1330] Nearest Common Ancestors (倍增法)

    题目同上篇,最近公共祖先. 因为没有清零tot,RE了好多次TAT 一定要初始化啊!! 1 #include<cstdio> 2 #include<cstring> 3 #in ...

  6. POJ - 1330 Nearest Common Ancestors tanjan_LCA

    传送门 题意就是题目 所谓在线,就是一个一个贼笨且时间太长. #include<stdio.h> #include<string.h> #include<iostream ...

  7. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

  8. 【POJ - 1330】Nearest Common Ancestors(lca,模板题)

    题干: A rooted tree is a well-known data structure in computer science and engineering. An example is ...

  9. LCA——JD 3055 Nearest Common Ancestors

    3055: Nearest Common Ancestors Time Limit: 1 Sec   Memory Limit: 128 MB Description 给定N个节点的一棵树,有K次查询 ...

最新文章

  1. OpenCV On Android
  2. golang确保输入过滤
  3. php关闭warning
  4. Sublime Text
  5. 【oracle】查看数据库最近执行了哪些sql语句
  6. 在IDEA中关于项目java版本问题
  7. 比特币交易手续费_投资人跑步进军比特币,OKEx交易手续费最高打6折
  8. 及时复盘的好处_如何做好2020的年终复盘?
  9. Card Trick(模拟)
  10. jQuery知识(转)
  11. 三运放差分放大电路分析_运放19——三运放仪表放大器工作原理分析
  12. 如何获取节假日的方法
  13. python期权定价公式_一揽子欧洲期权蒙特卡洛定价(python)(一)
  14. 苹果手机上网速度慢_是什么原因导致手机网速慢!
  15. 愿得一心人:硅谷亿万富豪们的婚姻怎样?有人白首相守七十年
  16. 新员工碰到新问题 公司论坛帮解决
  17. RNA-seq——快速下载SRA数据、解决fq文件中测序质量全为 ‘?‘ 的问题
  18. 古早软件 vim的使用
  19. 计算机经验交流活动简报,经验交流会简讯.doc
  20. java生成db文件

热门文章

  1. 上传服务器响应失败,Django CKEditor 上传图片提示“不正确的服务器响应”的解决办法...
  2. codeforces-constructive algorithms(构造算法.)
  3. 概率与计算机论文,数学概率统计论文范文
  4. [python]一个遍历多层文件夹,然后替换文件内容和目录名称的案例
  5. apache 配置 中英
  6. 转错误 x error LNK1104: 无法打开文件“E:\xxxx\Debug\xxxx.exe”
  7. 【QT5.3】VS2013+QT5.3 中文乱码问题 解决方案
  8. github速成手册
  9. [原创]一个shell小案例
  10. 被Google收购的Postini