poj1330:http://poj.org/problem?id=1330

题意:求一棵树上的两点的最近的公共祖先。

题解:第一次接触LCA,第一道模板题。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MAXN 10001
int n,fa[MAXN];
int indegree[MAXN];
int vis[MAXN];
vector<int> hash[MAXN];
struct Node{
int v;
int next;
int id;
}edge[MAXN*];
int head[MAXN],cnt;
int ances[MAXN],ans[MAXN];//祖先
void init(){
memset(head,-,sizeof(head));
memset(ans,-,sizeof(ans));
cnt=;
}
void add(int u,int v,int w){
edge[cnt].v=v;
edge[cnt].id=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].id=w;
edge[cnt].next=head[v];
head[v]=cnt++;
} void init(int n){
for(int i=;i<=n;i++){
fa[i]=i;
indegree[i]=;
vis[i]=;
ances[i]=;
hash[i].clear();
// Qes[i].clear();
}
}
int find(int x){
int s;
for(s=x;s!=fa[s];s=fa[s]);
while(s!=x){
int temp=fa[x];
fa[x]=s;
x=temp;
}
return s;
}
void unio(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy) return ;
fa[fy]=fx;
}
void Tarjan(int u){
ances[u]=u;
int i,size = hash[u].size();
for(i=;i<size;i++){
Tarjan(hash[u][i]);
unio(u,hash[u][i]);
ances[find(u)]=u;
}
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(vis[v]==){
ans[edge[i].id]=ances[find(v)];
return;
}
} /*size = Qes[u].size();
for(i=0;i<size;i++){
if(vis[Qes[u][i]]==1){
printf("%d\n",ances[find(Qes[u][i])]);
return;
}
}*/
}
int main(){
int t;
int i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init(n);
int s,d;
for(i=;i<=n-;i++){
scanf("%d%d",&s,&d);
hash[s].push_back(d);
indegree[d]++;
}
scanf("%d%d",&s,&d);
init();
add(s,d,);
add(d,s,);
for(j=;j<=n;j++){
if(indegree[j]==){
Tarjan(j);
break;
}
}
printf("%d\n",ans[]);
}
return ;
}

Nearest Common Ancestors的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  2. [最近公共祖先] POJ 1330 Nearest Common Ancestors

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

  3. POJ 1330 Nearest Common Ancestors

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

  4. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  5. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  6. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

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

  7. POJ 1330 Nearest Common Ancestors LCA题解

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

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

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

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

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

  10. pku 1330 Nearest Common Ancestors LCA离线

    pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...

随机推荐

  1. iOS截取视频缩略图的两种方法

    前言: 看完了使用MPMoviePlayerController播放在线视频,在实际应用中有时候须要获取视频的缩略图,我们来看看怎样截取指定时间内的视频缩略图. 一  使用MPMoviePlayerC ...

  2. WCF - 序列化

    数据是信息的载体 在不同环境中有不同的类型 为保证处于不同平台的的应用能够正常的进行数据交互 必须采用一种双方都能理解的数据类型 XML无疑是最好的选择 但不是唯一的选择 例如JSON也是一种普遍认可 ...

  3. 【Android】数据的应用-使用sharedpreferences存储数据

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  4. 阿里云服务器如何安装memcached

    方法/步骤 1 使用Xshell登陆阿里云服务器. 请使用root帐号登陆.下面的操作全部在home目录里执行 2 安装libevent. 输入命令 yum -y install libevent-d ...

  5. C#获取类中所有方法

    var t = typeof(HomeController); //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); // ...

  6. oracle批量导入数据

    关键代码 OracleDataAdapter da=new OracleDataAdapter(); string sql_select = string.Format("select id ...

  7. animationWithKeyPath键值对

    animationWithKeyPath键值对的方式来改变动画 <Jacky Shin:可以从这个网址查到哪些可以做为动画效果, 打开xcode帮助,搜索animatable propertie ...

  8. C# div、css

    目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...

  9. Delphi ControlCount和ComponentCount的区别

    ComponentCount指打开的窗体所拥有的控件个数,包含所有子组件.孙组件(子组件内的子组件) 如上图,Form1的ComponentCount是13,而Panel1的ComponentCoun ...

  10. MySQL增删改查的常用操作指令总结

    总结: 1.数据库操作: 创建库: create database db_name; 查询库: show databases; //显示所有的数据库 show create databases db_ ...