POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
该算法的详细解释请戳:
http://www.cnblogs.com/Findxiaoxun/p/3428516.html
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
;
int father[MAXN],ancestor[MAXN];
bool visit[MAXN];
int ans[MAXN];
vector<int> map[MAXN];//save the tree
vector<int> query[MAXN];//save the query
int n,t,root;
bool indegree[MAXN];//the indegree to find the root
int getfather(int v){//path compression
if(father[v]==v)return v;
return father[v]=getfather(father[v]);
}
void aunion(int u,int v){//??
int fv=getfather(v),fu=getfather(u);
father[fv]=fu;
}
void LCA(int id){
int len=map[id].size();
;i<len;i++){
int son=map[id][i];
LCA(son);
aunion(id,son);
}
visit[id]=;
len=query[id].size();
;i<len;i++){
int son=query[id][i];
if(visit[son])
ans[father[getfather(son)]]++;
}
}
void init(){
int x,y,z;
;i<=n;i++){
map[i].clear();
query[i].clear();
}
memset(visit,,sizeof(visit));
memset(ans,,sizeof(ans));
memset(indegree,,sizeof(indegree));
;i<=n;i++)father[i]=i;
;i<n;i++){
scanf("%d:(%d)",&x,&y);
;j<y;j++){
scanf("%d",&z);
indegree[z]=;
map[x].push_back(z);
}
}
scanf("%d",&t);
while(t--){//this method of the init is really clever
while(getchar()!='(');
scanf("%d%d",&x,&y);
query[x].push_back(y);
query[y].push_back(x);
}
while(getchar()!=')');
;i<=n;i++)if(!indegree[i])root=i;//find the root;warning:the 0
}
void output(){
;i<=n;i++){
)
printf("%d:%d\n",i,ans[i]);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
LCA(root);
output();
}
;
}
POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan的更多相关文章
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...
- POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- 求最近公共祖先(LCA)的各种算法
水一发题解. 我只是想存一下树剖LCA的代码...... 以洛谷上的这个模板为例:P3379 [模板]最近公共祖先(LCA) 1.朴素LCA 就像做模拟题一样,先dfs找到基本信息:每个节点的父亲.深 ...
- HihoCoder 1067 最近公共祖先(ST离线算法)
最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个 ...
随机推荐
- 常见编码和编码头BOM
ANSI(American National Standards Institute,美国国家标准学会)ANSI编码标准是指所有从基本ASCII码基础上发展起来的编码标准,比如扩展的ASCII码(12 ...
- 巧用set比较大小,缩短时间复杂度
struct Node { long long a; long long b; long long c; long long num; int i; bool operator < (const ...
- [C++]VAssistX文件头添加注释功能设置
问题情况:每次手写注释太慢,而且不统一.问题原因:C++可以利用宏写注释,也可以使用VAssistX提供的方法.问题处理:1.安装VAssistX在VS2010上2.VS2010菜单->VAss ...
- ipipe-3.10
git://git.xenomai.org/ipipe-gch.git for-ipipe-3.10
- linux进程同步机制_转
转自:Linux进程同步机制 具体应用可参考:线程同步 IPC之信号量 为了能够有效的控制多个进程之间的沟通过程,保证沟通过程的有序和和谐,OS必须提供一 定的同步机制保证进程之间不会自说 ...
- 解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题
症状: Windows操作系统,eclipse开发环境下,在本机上使用http://localhost:8080/...访问本机上的页面,使用tomcat作为服务器 在Servlet或者Action中 ...
- latex之图表位置控制
\begin{table}[!htbp] !-忽略“美学”标准 h-here t-top b-bottom p-page-of-its-own
- MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...
- 《Programming with Objective-C》第七章 Values and Collections
1.平台相关的数据类型 These types, like NSInteger and NSUInteger, are defined differently depending on the tar ...
- MFC获取系统当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...