POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
id=1330
给一个有根树,一个查询节点(u,v)的近期公共祖先
836K 16MS
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
const int maxn=10005;
using namespace std;
int n,m;//结点数,查询数
int f[maxn];//并查集
bool vis[maxn];//訪问标记
int ancestor[maxn];//祖先
int answer[2];//存储最后的查询结果
int in[maxn];//入度
vector<int> que[maxn];
struct Edge{
int to;
int next;
}edge[maxn<<1];int head[maxn],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void Init(){
tot=0;
memset(in,0,sizeof(in));
memset(f,-1,sizeof(f));
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
memset(ancestor,0,sizeof(ancestor));
for(int i=1;i<=n;++i){
que[i].clear();
}
}
int find(int x){
return f[x]==-1 ?
x:f[x]=find(f[x]);
}
void Union(int u,int v){
int t1=find(u);
int t2=find(v);
if(t1!=t2) f[t2]=t1;
}
void LCA(int u){
ancestor[u]=u;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(vis[v]) continue;
LCA(v);
Union(u,v);
ancestor[find(v)]=u;//可省略
}
vis[u]=true;
int sz=que[u].size();
for(int i=0;i<sz;++i){
int v=que[u][i];
if(vis[v]){
printf("%d\n",ancestor[find(v)]);//可替换find(v)
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,u,v,w;
cin>>T;
while(T--){
scanf("%d",&n);
Init();
for(int i=0;i<n-1;++i){
scanf("%d%d",&u,&v);
if(u!=v){
addedge(u,v);
in[v]++;
}
}
scanf("%d%d",&u,&v);
que[u].push_back(v);
que[v].push_back(u);
for(int i=1;i<=n;++i){
if(in[i]==0){
LCA(i);
break;
}
}
}
return 0;
}
POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)的更多相关文章
- POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...
- 图论-最近公共祖先-离线Tarjan算法
有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- POJ 1330 LCA最近公共祖先 离线tarjan算法
题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...
- POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...
- [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...
随机推荐
- 配置Struts2的异常处理
最好的方式是通过声明管理异常处理 1Action里面的execute()方法抛出所有异常:public String execute() throws Exception{} 2声明异常捕捉 ...
- thinkphp我掉下的一些坑
1.数据库连接,需要连接没有数据表前缀 如下,第二个参数必须为null,我之前写的是'',结果给我默认加了前缀 $User = M('User',Null,'DB_CONFIG2'); 2.AJAX获 ...
- 学渣也要搞 laravel(3)—— HTTP控制器
1. laravel 控制器在app/Http/Controllers/ 下,你会看到里面有一个Controller.php ,之后我们创建的控制器都是继承这个总控制器的.创建控制器只需要在这里面添加 ...
- Day20 Django之Model多对多、中间件、缓存、信号和分页
一.Form补充 class IndexForm(forms.Form): # c = [ # (1, 'CEO'), # (2, 'CTO') # ] # 静态字段,属于IndexForm类,即使数 ...
- 基于page的简单页面推送技术
我们可以先看下简单效果,打开2个页面可以看到推送效果 服务端我们只需要下面一个方法 using System; using System.Collections.Generic; using Syst ...
- 如何通过数据库修改WordPress后台登录密码
大家是否有过因为忘记WordPress后台登陆密码的时候?其实WordPress后台登陆密码的找回或修改的方法有多种,比如通过邮箱重启密码,又或者通过主机控制面板进入数据库修改等等.本篇教程以GoDd ...
- Codeforces Round #205 (Div. 2) : C
感觉像是一个数位dp,高位的1如果不选的话,前面低位的数都可以选:不然只能选择为1的数: 代码: #include<iostream> #include<algorithm> ...
- 有感,懂市场比懂产品重要,懂产品比懂技术重要——想起凡客诚品和YY语音了
一个创业公司,最好三样都要有,但应该CEO是懂市场,经理懂产品,程序员最好懂技术厉害一点-这还不算,销售也要厉害一点,不能守株待兔- 美工——有钱最好请个美工,最起码也要请人设计修改一下- 财务——不 ...
- Bluetooth LE(低功耗蓝牙) - 第五部分
回顾: 在本系列前面的文章中我们完成了发现BLE传感器并与之建立连接.现在只剩下从其中获取数据了,但是这并没有看起来那么简单.在这篇文章中我们将讨论GATT的特点以及如何促进主机与传感器之间的数据交换 ...
- 在ubuntu14.04 64位中使用jd-gui
使用时提示缺少库,输入命令sudo apt-get install libgtk2.0-0:i386 libxxf86vm1:i386 libsm6:i386 lib32stdc++6 参考:http ...