Nearest Common Ancestors
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的更多相关文章
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14698 Accept ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- pku 1330 Nearest Common Ancestors LCA离线
pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...
随机推荐
- ICOPclient版本号,异步connect
之前在网上看到一个服务端的ICOP模块,比較小巧,感觉还不错,后来在工作中,需要开发一个挂号的程序,监视大量server执行情况,初期连接数大概六七百,我就把这个ICOP模块改造成了一个client版 ...
- 计算机体系结构-内存调优IPC OOMK
man ipc [root@server1 proc]# man ipcIPC(2) Linux Programmer’s Manual ...
- iOS-CALayer中position与anchorPoint详解
iOS-CALayer中position与anchorPoint详解 属性介绍 CALayer通过四个属性来确定大小和位置, 分别为:frame.bounds.position.anchorPoint ...
- WKWebView使用过程中的那些坑
问题产生背景: 新开发的页面中有一部分的界面是需要展示后端接口返回的HTML代码,包括文字和图片.所以就自然而然的要使用iOS原生的WebKit. 鉴于Xcode 8发布以后,编译器支持的最低版本(D ...
- Conversion Between DataTable and List in C#
1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...
- DNS服务器的原理
当用户去访问一个网站(百度)的时候,首先去请求dns服务器,根据对应的域名返回所在ip,然后再使用ip去访问自己所在服务器空间.简单的说,DNS服务器就像114客服,dns服务器是树状结构的,dns服 ...
- DOM - nodeType 的取值
DOM 中,共有 12 中不同类型的节点,nodeType 的取值以数值表示. 节点类型 描述 子节点 1 Element 表示元素. Element, Text, Comment, Proce ...
- datazen 修改instanceid db_encryption_key
切换到Datazen.Enterprise.Server.3.0.3327.exe 所在的目录 运行如下命令: Datazen.Enterprise.Server.3.0.3327.exe DATAZ ...
- C#超时处理(转载)
/// <summary> /// 超时处理 /// /// /// </summary> public class TimeoutChecker ...
- JVM Run-Time Data Areas.
Ref: JVM Run-Time Data Areas class SimpleThread extends Thread { public SimpleThread(String name) { ...