题目大意:唔 就是给你一棵树 和两个点,问你这两个点的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. freebsd自动获取ip地址

    最小化安装完成freebsd后,ifconfig查看不到ip地址 修改/etc/rc.conf 添加ifconfig_网卡名称="DHCP" 重启服务器或者sh /etc/rc.c ...

  2. 洛谷 P1629 邮递员送信

    题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每 ...

  3. DP一直是自己的弱势 开始练滚动数组——HDOJ4502

    http://acm.hdu.edu.cn/showproblem.php?pid=4502//题目链接 思路 : dp[i]表示 到第i天能获得的最大工资  依次更新 #include<cst ...

  4. Itunes共享机制实现

    http://www.raywenderlich.com/1948/itunes-tutorial-for-ios-how-to-integrate-itunes-file-sharing-with- ...

  5. 安装VC++6.0实验环境

    安装VC++6.0步骤:(1)下载一个压缩包进行解压(2)点击打开解压后的文件(3)找到文件里的程序进行安装(4)等待安装完成该程序后可以试着运行一下此程序,在此我们需要了解编写程序的步骤和注意事项. ...

  6. javase(12)_集合框架_Queue

    一.Queue Queye接口体系图 体系分析: Deque实现类:ArrayDeque, LinkedList(数组和链表实现双向队列) BlockingDeque实现类:LinkedBlockin ...

  7. Bzoj 1055: [HAOI2008]玩具取名 (区间DP)

    Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...

  8. 【Linux命令大全】

    Linux常用命令大全 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部 ...

  9. shell脚本中使用echo显示带颜色的内容

    shell脚本中使用echo显示带颜色的内容,需要使用参数-e 格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 例如: echo -e &qu ...

  10. Perl学习之四:语句

    语句:if/unless while/foreach/do..while/for 1.表达式真价值总结任何表达式都有真假值:逻辑.字符串.列表.文件 2.if if(expression1){ sta ...