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 ...
随机推荐
- spring mvc DispatcherServlet详解之拾忆工具类utils
DispatcherServlet的静态初始化 /** * Name of the class path resource (relative to the DispatcherServlet cla ...
- 覆盖equals的时候总要覆盖hashCode
import java.util.HashMap; public class Student { private String name ; private String id; public Stu ...
- Java源码中的发现:快速判断一个int值是几位数
判断一个int值是几位数,要是我自己实现,估计又会想到除法和模运算了,偶然在java标准API源码中发现的写法,很强大. public class Test { final static int[] ...
- java按照集合中元素的属性进行排序示例代码
public class Student { private String name; private int age; private int id; public Student() { sup ...
- 【Android】数据的应用-使用sharedpreferences存储数据
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
- ToString格式.
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- windows 20003 扩展安装后不成功的原因
windows扩展如果安装不成功(PHP扩展)很大的可能就是那个DLL的权限不够.需要分配:AdministratorAuthenticater UsersIIS_WPGSYSTEMUsers
- iOS如何准确获取通知
iOS获取通知需要注意以下三个地方iOS 设备收到一条推送(APNs),用户点击推送通知打开应用时,应用程序根据状态不同进行处理需在 AppDelegate 中的以下两个方法中添加代码以获取apn内容 ...
- 在万网虚拟主机上部署MVC5
参考 要想部署mvc,需要把一些mvc用到的全局程序集改为本地部署,通过N次试验,终于搞定. 特写个备忘录,免得以后忘了. 首先更改web.config,在里面加上 <system.web> ...