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 ...
随机推荐
- github入门之基本操作--4
1.初始化仓库 如果成功执行git init 命令,该目录下会生成一个.git的目录 2.查看仓库状态 *注: 实际工作中,git status使用次数非常多,一定要记住.因为当工作树和仓库被操作的过 ...
- 实训随笔:EL表达式JSON应用
由于之前在学校写的jsp页面都是夹杂着java代码的,所以之前写了个jsp,满满的<%%>和java代码,老师说那样太不美观了啊!!!要全部用EL表达式替代了.本人还是太笨了,弄了一上午才 ...
- Selenium私房菜系列7 -- 玩转Selenium Server
本篇主要是想更进一步介绍Selenium Server的工作原理,这次我们从Selenium Server的交互模式开始. 在<第一个Selenium RC测试案例>中,我们以命令“jav ...
- SQL,数据库连接
- 树形dp——Tree2cycle
一.问题描述(题目链接) 给你一棵树,删除或添加一条边的费用都是1,问使它变成一个环的最小费用. 二.解题思路 回溯法,然后回溯的时候的当前节点度数>2(如果是成环的话肯定就是2或者小于2)就把 ...
- JavaScript中对象的属性:如何遍历属性
for/in 语句循环遍历对象的属性. js中获取key得到某对象中相对应的value的方法:obj.key js中根据动态key得到某对象中相对应的value的方法有二: 一.var key = & ...
- [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)
题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...
- javaEE(8)_EL表达式语言
一.EL表达式简介 EL全名为Expression Language.EL主要作用: 1>获取数据: •EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域中检索java对象. ...
- 看结果,测试?java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 参数传递?
看结果1? package com.swift; class ArrayString { public static void main(String[] args) { String str = & ...
- JS添加验证页面中script标签中是否存在jquery文件
window.onload = function() { var al = document.getElementsByTagName("script"); var new_ele ...