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用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个 ...
随机推荐
- linux超级终端minicom的使用方法
===== 一.Minicom介绍 ===== Linux下的Minicom的功能与Windows下的超级终端功能相似,可以通过串口控制外部的硬件 设备.适于在linux通过超级终端对 ...
- floyd算法&迪杰斯特拉算法
; k<=n; k++) ; i<=n; i++) ; j<=n; j++) { gra[i][j]=min(gra[i][j],gra[i][k]+gra[k][j]); } vo ...
- 记一次线上MySQL数据库死锁问题
最近线上项目报了一个MySQL死锁(DealLock)错误,虽说对业务上是没有什么影响的,由于自己对数据库锁这块了解不是很多,之前也没怎么的在线上碰到过.这次刚好遇到了,便在此记录一下 ...
- 为什么 Windows API 使用 stdcall 调用约定?
作者:知乎用户链接:https://www.zhihu.com/question/31453641/answer/52001143来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- UCOS2系统内核讲述(五)_初始化TCB详情
Ⅰ.写在前面 学习本文之前可以参看我前面的文章: UCOS2系统内核讲述(四)_创建任务 上一篇文章讲述了关于函数“OSTaskCreateExt”创建任务函数体里面重要一些的内容,本文接着上一张讲述 ...
- oozie调度hive脚本demo
1. 环境配置 2. 脚本配置 3. 执行job 4. 查看结果 待发布 ..
- myeclipse工程重名后怎么更改deploy location?
http://zhidao.baidu.com/link?url=I9E16OYfxovPHqBrRWhYCI9TYNG_X-Whg_X7QrJiOBXBGEwi-6WYsC-Zi4Jcg9zd3ye ...
- 近似推断(Approximate Inference)
1.变分推断(Variational Inference) 1.1.分解概率分布(Factorized distributions) 1.2.分解近似的性质(Properties of factori ...
- 【BZOJ】1696: [Usaco2007 Feb]Building A New Barn新牛舍(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1696 原题要求min(sum{|x-xi|+|y-yi|}),且一定要看题:“没有两头牛的吃草位置是 ...
- nginx php文件上传的大小配置问题