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用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个 ...
随机推荐
- 算法 quick sort
// ------------------------------------------------------------------------------------------------- ...
- 干接点&湿接点
干接点的定义: 无源开关:具有闭合和断开的2种状态:2个接点之间没有极性,可以互换: 常见的干接点信号有: 1.各种开关如:限位开关.行程开关.脚踏开关.旋转开关.温度开关.液位开关等: 2.各种按键 ...
- 工作中Hadoop,Spark,Phoenix,Impala 集群中遇到坑及解决方案
1.HDFS 修复 问题描述:其他部门在yarn平台上跑spark 程序错误的生成了海量的不到100K的小文件,导致namenode压力过大,其中一个namenode宕机后,没有及时发现 使得edit ...
- Entity Framework(四):使用DbModelBuilder API创建表结构
DbContext类有一个OnModelCreating方法,它用于流利地配置领域类到数据库模式的映射.下面我们以fluent API的方式来定义映射.首先,先将Product类注释掉,重新编写该类, ...
- springMVC介绍
http://www.iteye.com/blogs/subjects/springMVC —————————————————————————————————————————————————————— ...
- jira集成fisheye代码深度查看工具安装绿色版
软件介绍: Fisheye 是一个源代码库深度查看软件,它可以挖掘源代码库中的有用信息,呈现在Web浏览器界面上,Fisheye优点: 1) Fisheye是一个基于Web的代码管理系统,可以与SVN ...
- 按SCI影响因子排序的前50人工智能期刊列表
附录二:按SCI影响因子排序的前50人工智能期刊列表 出版物名称,影响因子 IEEE TRANSACTIONS ON FUZZY SYSTEMS, 6.701 International Jou ...
- web 开发之nginx--- 阿里云部署nginx
http://blog.csdn.net/zhangjingyangguang/article/details/7441268 http://www.cnblogs.com/languoliang/a ...
- swift - UIView 设置背景色和背景图片
代码如下: let page = UIView() page.frame = self.view.bounds //直接设置颜色 page.backgroundColor = UIColor.gree ...
- Python Scrapy 自动爬虫注意细节(3)
一.对指定页面爬取 yield Request(url, meta={'cookiejar': response.meta['cookiejar']}, callback=self.parse_url ...